Skip to content
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
31 changes: 27 additions & 4 deletions Sources/RealModule/Angle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
///
/// All trigonometric functions expect the argument to be passed as radians (Real), but this is not enforced by the type system.
/// This type serves exactly this purpose, and can be seen as an alternative to the underlying Real implementation.
public struct Angle<T: Real> {
public struct Angle<T: Real>: Equatable {
public var radians: T
public init(radians: T) { self.radians = radians }
public static func radians(_ val: T) -> Angle<T> { .init(radians: val) }
Expand Down Expand Up @@ -160,9 +160,32 @@ public extension Angle {
}
}

extension Angle: Equatable {
public static func == (lhs: Angle<T>, rhs: Angle<T>) -> Bool {
lhs.radians.isApproximatelyEqual(to: rhs.radians)
public extension Angle {
/// Checks whether the current angle is contained within a given closed range.
///
/// - Parameters:
///
/// - range: The closed angular range within which containment is checked.
func contained(in range: ClosedRange<Angle<T>>) -> Bool {
Copy link
Contributor

@xwu xwu Dec 14, 2020

Choose a reason for hiding this comment

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

What is the value of these APIs over what's written in the body? I would suggest not including these separately; the existing forms are canonical and straightforward enough to write.

What @NevinBR is getting at, I think, is that 361 degrees is contained "between" 0 degrees and 2 degrees. This does not offer that functionality, and convenient access to it would be useful. It would need to be distinguished from the functionality here, though, through some thoughtful naming.

For API naming purposes, incidentally, the function would need to be named isContained; and for style purposes, the access modifier public should be specified on each member (and therefore is not necessary on the extension itself).

Copy link
Contributor

Choose a reason for hiding this comment

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

What @NevinBR is getting at, I think, is that 361 degrees is contained "between" 0 degrees and 2 degrees. This does not offer that functionality.

Additionally, 175º and -175º are both “between” 170º and -170º (but they are not between -170º and 170º).

Copy link
Author

@jkalias jkalias Dec 14, 2020

Choose a reason for hiding this comment

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

So, essentially you mean @NevinBR that the containment should be performed on the normalized angular range, right?

361º -> normalized to 1º -> contained in -10º...10º
361º -> normalized to 1º -> not contained in 10º...-10º
175º -> normalized to 175º -> contained in -170º...170º
-175º -> normalized to -175º -> contained in -170º...170º
175º -> normalized to 175º -> not contained in 170º...-170º
-175º -> normalized to -175º -> not contained in 170º...-170º

Copy link
Contributor

Choose a reason for hiding this comment

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

I’m not entirely sure how normalization should come into play. Probably yes normalized angles should be used for comparison / containment, and if someone wants to work with the unnormalized value they can extract it and do so manually.

However, several of your examples do not behave as I would expect:

175º -> normalized to 175º -> contained in -170º...170º
-175º -> normalized to -175º -> contained in -170º...170º
175º -> normalized to 175º -> not contained in 170º...-170º
-175º -> normalized to -175º -> not contained in 170º...-170º

These should be:

175º -> normalized to 175º -> not contained in -170º...170º
-175º -> normalized to -175º -> not contained in -170º...170º
175º -> normalized to 175º -> yes contained in 170º...-170º
-175º -> normalized to -175º -> yes contained in 170º...-170º

range.contains(self)
}

/// Checks whether the current angle is contained within a given half-open range.
///
/// - Parameters:
///
/// - range: The half-open angular range within which containment is checked.
func contained(in range: Range<Angle<T>>) -> Bool {
range.contains(self)
}
}

extension Angle: Comparable {
public static func < (lhs: Angle<T>, rhs: Angle<T>) -> Bool {
guard lhs != rhs else {
return false
}
return lhs.radians < rhs.radians
}
}

Expand Down
20 changes: 18 additions & 2 deletions Tests/RealTests/AngleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ where Self: BinaryFloatingPoint {

static func additiveArithmeticTests() {
var angle = Angle(degrees: 30)
XCTAssertEqual(Angle(degrees: 50), angle + Angle(degrees: 20))
XCTAssertEqual(Angle(degrees: 10), angle - Angle(degrees: 20))
assertClose(50, (angle + Angle(degrees: 20)).degrees)
assertClose(10, (angle - Angle(degrees: 20)).degrees)
XCTAssertEqual(Angle(degrees: 60), angle * 2)
XCTAssertEqual(Angle(degrees: 60), 2 * angle)
XCTAssertEqual(Angle(degrees: 15), angle / 2)
Expand All @@ -121,6 +121,18 @@ where Self: BinaryFloatingPoint {
angle /= 6
XCTAssertEqual(Angle(degrees: 10), angle)
}

static func rangeContainmentTests() {
let angle = Angle(degrees: 30)
XCTAssertTrue(angle.contained(in: Angle(degrees: 10)...Angle(degrees: 40)))
XCTAssertTrue(angle.contained(in: Angle(degrees: 10)...Angle(degrees: 30)))
XCTAssertTrue(angle.contained(in: Angle(degrees: 30)...Angle(degrees: 40)))
XCTAssertFalse(angle.contained(in: Angle(degrees: 10)...Angle(degrees: 20)))
XCTAssertFalse(angle.contained(in: Angle(degrees: 50)...Angle(degrees: 60)))
XCTAssertTrue(angle.contained(in: Angle(degrees: 30)..<Angle(degrees: 40)))
XCTAssertFalse(angle.contained(in: Angle(degrees: 10)..<Angle(degrees: 30)))
XCTAssertTrue(angle.contained(in: Angle(degrees: 10)..<Angle(degrees: 40)))
}
}

final class AngleTests: XCTestCase {
Expand All @@ -131,6 +143,7 @@ final class AngleTests: XCTestCase {
Float16.trigonometricFunctionChecks()
Float16.specialDegreesTrigonometricFunctionChecks()
Float16.additiveArithmeticTests()
Float16.rangeContainmentTests()
}
}
#endif
Expand All @@ -140,13 +153,15 @@ final class AngleTests: XCTestCase {
Float.trigonometricFunctionChecks()
Float.specialDegreesTrigonometricFunctionChecks()
Float.additiveArithmeticTests()
Float.rangeContainmentTests()
}

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

#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
Expand All @@ -155,6 +170,7 @@ final class AngleTests: XCTestCase {
Float80.trigonometricFunctionChecks()
Float80.specialDegreesTrigonometricFunctionChecks()
Float80.additiveArithmeticTests()
Float80.rangeContainmentTests()
}
#endif
}