11import simd
2+ #if !canImport(Darwin)
3+ // If we don't have Apple-specific intrinsics, use Glibc instead
4+ import Glibc
5+ #endif
26
37/// A floating point value that represents an angle
48
@@ -11,6 +15,11 @@ public struct Angle {
1115 public var radians : Float32 {
1216 degrees * Float32. pi / 180.0
1317 }
18+
19+ @inline ( __always)
20+ public var piFactor : Float {
21+ degrees / 180.0
22+ }
1423
1524 /// Creates an instance using the value in radians
1625 /// - Parameter radians: The angle value in radians
@@ -208,7 +217,13 @@ extension Angle: Comparable {
208217/// - sina: A reference to a variable to store the sine of the angle
209218/// - cosa: A reference to a variable to store the cosine of the angle
210219public func sincos( _ a: Angle , _ sina: inout Float , _ cosa: inout Float ) {
211- __sincospif ( a. degrees / 180.0 , & sina, & cosa)
220+ #if canImport(Darwin)
221+ __sincospif ( a. piFactor, & sina, & cosa)
222+ #else
223+ let r = a. radians
224+ sina = sinf ( r)
225+ cosa = cosf ( r)
226+ #endif
212227}
213228
214229/// Computes the sine and cosine of the given angle
@@ -218,27 +233,38 @@ public func sincos(_ a: Angle) -> (sin: Float, cos: Float) {
218233 var s : Float = 0.0
219234 var c : Float = 0.0
220235 sincos ( a, & s, & c)
221-
222236 return ( sin: s, cos: c)
223237}
224238
225239/// Computes the sine of the given angle
226240/// - Parameter a: The angle
227241/// - Returns: The sine of the angle
228242public func sin( _ a: Angle ) -> Float {
229- __sinpif ( a. degrees / 180.0 )
243+ #if canImport(Darwin)
244+ return __sinpif ( a. piFactor)
245+ #else
246+ return sinf ( a. radians)
247+ #endif
230248}
231249
232250/// Computes the cosine of the given angle
233251/// - Parameter a: The angle
234252/// - Returns: The cosine of the angle
235253public func cos( _ a: Angle ) -> Float {
236- __cospif ( a. degrees / 180.0 )
254+ #if canImport(Darwin)
255+ return __cospif ( a. piFactor)
256+ #else
257+ return cosf ( a. radians)
258+ #endif
237259}
238260
239261/// Computes the tangent of the given angle
240262/// - Parameter a: The angle
241263/// - Returns: The tangent of the angle
242264public func tan( _ a: Angle ) -> Float {
243- __tanpif ( a. degrees / 180.0 )
265+ #if canImport(Darwin)
266+ return __tanpif ( a. piFactor)
267+ #else
268+ return tanf ( a. radians)
269+ #endif
244270}
0 commit comments