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 7 commits
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
86 changes: 78 additions & 8 deletions Sources/RealModule/Angle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,87 @@
///
/// 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 & BinaryFloatingPoint> {
public var radians: T
public init(radians: T) { self.radians = radians }
public static func radians(_ val: T) -> Angle<T> { .init(radians: val) }

public var degrees: T { radians * 180 / .pi }
public init(degrees: T) { self.init(radians: degrees * .pi / 180) }
public init(degrees: T) {
let normalized = normalize(degrees, limit: 180)
self.init(radians: normalized * .pi / 180)
}
public static func degrees(_ val: T) -> Angle<T> { .init(degrees: val) }
}

public extension Angle {
public extension ElementaryFunctions
where Self: Real & BinaryFloatingPoint {
/// See also:
/// -
/// `ElementaryFunctions.cos()`
var cos: T { T.cos(radians) }
static func cos(_ angle: Angle<Self>) -> Self {
let normalizedRadians = normalize(angle.radians, limit: .pi)

if -.pi/4 < normalizedRadians && normalizedRadians < .pi/4 {
return Self.cos(normalizedRadians)
}

if normalizedRadians > 3 * .pi / 4 || normalizedRadians < -3 * .pi / 4 {
return -Self.cos(.pi - normalizedRadians)
}

if normalizedRadians >= 0 {
return Self.sin(.pi/2 - normalizedRadians)
}

return Self.sin(normalizedRadians + .pi / 2)
}

/// See also:
/// -
/// `ElementaryFunctions.sin()`
var sin: T { T.sin(radians) }
static func sin(_ angle: Angle<Self>) -> Self {
let normalizedRadians = normalize(angle.radians, limit: .pi)

if .pi / 4 < normalizedRadians && normalizedRadians < 3 * .pi / 4 {
return Self.sin(normalizedRadians)
}

if -3 * .pi / 4 < normalizedRadians && normalizedRadians < -.pi / 4 {
return -Self.sin(-normalizedRadians)
}

if normalizedRadians > 3 * .pi / 4 {
return Self.sin(.pi - normalizedRadians)
}

if normalizedRadians < -3 * .pi / 4 {
return -Self.sin(.pi + normalizedRadians)
}

return Self.sin(normalizedRadians)
}

/// See also:
/// -
/// `ElementaryFunctions.tan()`
var tan: T { T.tan(radians) }

static func tan(_ angle: Angle<Self>) -> Self {
let sine = sin(angle)
let cosine = cos(angle)

guard cosine != 0 else {
var result = Self.infinity
if sine.sign == .minus {
result.negate()
}
return result
}

return sine / cosine
}
}

public extension Angle {
/// See also:
/// -
/// `ElementaryFunctions.acos()`
Expand All @@ -60,3 +115,18 @@ public extension Angle {
/// `RealFunctions.atan2()`
static func atan2(y: T, x: T) -> Self { Angle.radians(T.atan2(y: y, x: x)) }
}

private func normalize<T>(_ input: T, limit: T) -> T
where T: Real & BinaryFloatingPoint {
var normalized = input

while normalized > limit {
normalized -= 2 * limit
}

while normalized < -limit {
normalized += 2 * limit
}

return normalized
}
79 changes: 76 additions & 3 deletions Tests/RealTests/AngleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,79 @@ where Self: BinaryFloatingPoint {
assertClose(0.3843967744956390830381948729670469737, Angle<Self>.asin(0.375).radians)
assertClose(0.3587706702705722203959200639264604997, Angle<Self>.atan(0.375).radians)
assertClose(0.54041950027058415544357836460859991, Angle<Self>.atan2(y: 0.375, x: 0.625).radians)

assertClose(0.9305076219123142911494767922295555080, cos(Angle<Self>(radians: 0.375)))
assertClose(0.3662725290860475613729093517162641571, sin(Angle<Self>(radians: 0.375)))
assertClose(0.3936265759256327582294137871012180981, tan(Angle<Self>(radians: 0.375)))
}

static func specialDegreesTrigonometricFunctionChecks() {
assertClose(1, cos(Angle<Self>(degrees: -360)))
Copy link
Member

Choose a reason for hiding this comment

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

The tests with exact results should test exact equality, not assertClose.

assertClose(0, cos(Angle<Self>(degrees: -270)))
assertClose(-1, cos(Angle<Self>(degrees: -180)))
assertClose(-0.86602540378443864676372317075293618347, cos(Angle<Self>(degrees: -150)))
assertClose(-0.70710678118654752440084436210484903929, cos(Angle<Self>(degrees: -135)))
assertClose(-0.5, cos(Angle<Self>(degrees: -120)))
assertClose(0, cos(Angle<Self>(degrees: -90)))
assertClose(0.5, cos(Angle<Self>(degrees: -60)))
assertClose(0.70710678118654752440084436210484903929, cos(Angle<Self>(degrees: -45)))
assertClose(0.86602540378443864676372317075293618347, cos(Angle<Self>(degrees: -30)))
assertClose(1, cos(Angle<Self>(degrees: 0)))
assertClose(0.86602540378443864676372317075293618347, cos(Angle<Self>(degrees: 30)))
assertClose(0.70710678118654752440084436210484903929, cos(Angle<Self>(degrees: 45)))
assertClose(0.5, cos(Angle<Self>(degrees: 60)))
assertClose(0, cos(Angle<Self>(degrees: 90)))
assertClose(-0.5, cos(Angle<Self>(degrees: 120)))
assertClose(-0.70710678118654752440084436210484903929, cos(Angle<Self>(degrees: 135)))
assertClose(-0.86602540378443864676372317075293618347, cos(Angle<Self>(degrees: 150)))
assertClose(-1, cos(Angle<Self>(degrees: 180)))
assertClose(0, cos(Angle<Self>(degrees: 270)))
assertClose(1, cos(Angle<Self>(degrees: 360)))

assertClose(0, sin(Angle<Self>(degrees: -360)))
assertClose(1, sin(Angle<Self>(degrees: -270)))
assertClose(0, sin(Angle<Self>(degrees: -180)))
assertClose(-0.5, sin(Angle<Self>(degrees: -150)))
assertClose(-0.70710678118654752440084436210484903929, sin(Angle<Self>(degrees: -135)))
assertClose(-0.86602540378443864676372317075293618347, sin(Angle<Self>(degrees: -120)))
assertClose(-1, sin(Angle<Self>(degrees: -90)))
assertClose(-0.86602540378443864676372317075293618347, sin(Angle<Self>(degrees: -60)))
assertClose(-0.70710678118654752440084436210484903929, sin(Angle<Self>(degrees: -45)))
assertClose(-0.5, sin(Angle<Self>(degrees: -30)))
assertClose(0, sin(Angle<Self>(degrees: 0)))
assertClose(0.5, sin(Angle<Self>(degrees: 30)))
assertClose(0.70710678118654752440084436210484903929, sin(Angle<Self>(degrees: 45)))
assertClose(0.86602540378443864676372317075293618347, sin(Angle<Self>(degrees: 60)))
assertClose(1, sin(Angle<Self>(degrees: 90)))
assertClose(0.86602540378443864676372317075293618347, sin(Angle<Self>(degrees: 120)))
assertClose(0.70710678118654752440084436210484903929, sin(Angle<Self>(degrees: 135)))
assertClose(0.5, sin(Angle<Self>(degrees: 150)))
assertClose(0, sin(Angle<Self>(degrees: 180)))
assertClose(-1, sin(Angle<Self>(degrees: 270)))
assertClose(0, sin(Angle<Self>(degrees: 360)))

assertClose(0, tan(Angle<Self>(degrees: -360)))
assertClose(Double.infinity, tan(Angle<Self>(degrees: -270)))
assertClose(0, tan(Angle<Self>(degrees: -180)))
assertClose(0.57735026918962576450914878050195745565, tan(Angle<Self>(degrees: -150)))
assertClose(1, tan(Angle<Self>(degrees: -135)))
assertClose(1.7320508075688772935274463415058723669, tan(Angle<Self>(degrees: -120)))
assertClose(-Double.infinity, tan(Angle<Self>(degrees: -90)))
assertClose(-1.7320508075688772935274463415058723669, tan(Angle<Self>(degrees: -60)))
assertClose(-1, tan(Angle<Self>(degrees: -45)))
assertClose(-0.57735026918962576450914878050195745565, tan(Angle<Self>(degrees: -30)))
assertClose(0, tan(Angle<Self>(degrees: 0)))
assertClose(0.57735026918962576450914878050195745565, tan(Angle<Self>(degrees: 30)))
assertClose(1, tan(Angle<Self>(degrees: 45)))
assertClose(1.7320508075688772935274463415058723669, tan(Angle<Self>(degrees: 60)))
assertClose(Double.infinity, tan(Angle<Self>(degrees: 90)))
assertClose(-1.7320508075688772935274463415058723669, tan(Angle<Self>(degrees: 120)))
assertClose(-1, tan(Angle<Self>(degrees: 135)))
assertClose(-0.57735026918962576450914878050195745565, tan(Angle<Self>(degrees: 150)))
assertClose(0, tan(Angle<Self>(degrees: 180)))
assertClose(-Double.infinity, tan(Angle<Self>(degrees: 270)))
assertClose(0, tan(Angle<Self>(degrees: 360)))

assertClose(0.9305076219123142911494767922295555080, Angle<Self>(radians: 0.375).cos)
assertClose(0.3662725290860475613729093517162641571, Angle<Self>(radians: 0.375).sin)
assertClose(0.3936265759256327582294137871012180981, Angle<Self>(radians: 0.375).tan)
}
}

Expand All @@ -44,24 +113,28 @@ final class AngleTests: XCTestCase {
if #available(iOS 14.0, watchOS 14.0, tvOS 7.0, *) {
Float16.conversionBetweenRadiansAndDegreesChecks()
Float16.trigonometricFunctionChecks()
Float16.specialDegreesTrigonometricFunctionChecks()
}
}
#endif

func testFloat() {
Float.conversionBetweenRadiansAndDegreesChecks()
Float.trigonometricFunctionChecks()
Float.specialDegreesTrigonometricFunctionChecks()
}

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

#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
func testFloat80() {
Float80.conversionBetweenRadiansAndDegreesChecks()
Float80.trigonometricFunctionChecks()
Float80.specialDegreesTrigonometricFunctionChecks()
}
#endif
}
8 changes: 6 additions & 2 deletions Tests/RealTests/ElementaryFunctionChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ internal func assertClose<T>(
file: StaticString = #file,
line: UInt = #line
) -> T where T: BinaryFloatingPoint {
// we need to first check if the values are zero before we check the signs
// otherwise, "0" and "-0" compare as unequal (eg. sin(-180) == 0)
let expectedT = T(expected)
if observed.isZero && expectedT.isZero { return 0 }

// Shortcut relative-error check if we got the sign wrong; it's OK to
// underflow to zero, but we do not want to allow going right through
// zero and getting the sign wrong.
Expand All @@ -38,8 +43,7 @@ internal func assertClose<T>(
if observed.isNaN && expected.isNaN { return 0 }
// If T(expected) is zero or infinite, and matches observed, the error
// is zero.
let expectedT = T(expected)
if observed.isZero && expectedT.isZero { return 0 }

if observed.isInfinite && expectedT.isInfinite { return 0 }
// Special-case where only one of expectedT or observed is infinity.
// Artificially knock everything down a binade, treat actual infinity as
Expand Down