Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing Angle type [Issue #88] #169

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Sources/RealModule/Angle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public extension Angle {
}
}

extension Angle: Equatable {
public static func == (lhs: Angle<T>, rhs: Angle<T>) -> Bool {
lhs.radians.isApproximatelyEqual(to: rhs.radians)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementations of == cannot use approximate equality because it does not fulfill the semantic requirements of ==.

}
}

private func normalize<T>(_ input: T, limit: T) -> T
where T: Real {
var normalized = input
Expand Down
22 changes: 21 additions & 1 deletion Tests/RealTests/AngleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,23 @@ where Self: BinaryFloatingPoint {
XCTAssertEqual(0, tan(Angle<Self>(degrees: 180)))
XCTAssertEqual(-.infinity, tan(Angle<Self>(degrees: 270)))
XCTAssertEqual(0, tan(Angle<Self>(degrees: 360)))

}

static func additiveArithmeticTests() {
var angle = Angle(degrees: 30)
XCTAssertEqual(Angle(degrees: 50), angle + Angle(degrees: 20))
XCTAssertEqual(Angle(degrees: 10), angle - Angle(degrees: 20))
XCTAssertEqual(Angle(degrees: 60), angle * 2)
XCTAssertEqual(Angle(degrees: 60), 2 * angle)
XCTAssertEqual(Angle(degrees: 15), angle / 2)
angle += Angle(degrees: 10)
XCTAssertEqual(Angle(degrees: 40), angle)
angle -= Angle(degrees: 20)
XCTAssertEqual(Angle(degrees: 20), angle)
angle *= 3
XCTAssertEqual(Angle(degrees: 60), angle)
angle /= 6
XCTAssertEqual(Angle(degrees: 10), angle)
}
}

Expand All @@ -114,6 +130,7 @@ final class AngleTests: XCTestCase {
Float16.conversionBetweenRadiansAndDegreesChecks()
Float16.trigonometricFunctionChecks()
Float16.specialDegreesTrigonometricFunctionChecks()
Float16.additiveArithmeticTests()
}
}
#endif
Expand All @@ -122,19 +139,22 @@ final class AngleTests: XCTestCase {
Float.conversionBetweenRadiansAndDegreesChecks()
Float.trigonometricFunctionChecks()
Float.specialDegreesTrigonometricFunctionChecks()
Float.additiveArithmeticTests()
}

func testDouble() {
Double.conversionBetweenRadiansAndDegreesChecks()
Double.trigonometricFunctionChecks()
Double.specialDegreesTrigonometricFunctionChecks()
Double.additiveArithmeticTests()
}

#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
func testFloat80() {
Float80.conversionBetweenRadiansAndDegreesChecks()
Float80.trigonometricFunctionChecks()
Float80.specialDegreesTrigonometricFunctionChecks()
Float80.additiveArithmeticTests()
}
#endif
}