@@ -57,6 +57,16 @@ extension Float: Real {
5757 return libm_powf ( x, Float ( n) )
5858 }
5959
60+ @_transparent public static func root( _ x: Float , _ n: Int ) -> Float {
61+ guard x >= 0 || n % 2 != 0 else { return . nan }
62+ // Workaround the issue mentioned below for the specific case of n = 3
63+ // where we can fallback on cbrt.
64+ if n == 3 { return libm_cbrtf ( x) }
65+ // TODO: this implementation is not quite correct, because either n or
66+ // 1/n may be not be representable as Float.
67+ return Float ( signOf: x, magnitudeOf: libm_powf ( x. magnitude, 1 / Float( n) ) )
68+ }
69+
6070 @_transparent public static func atan2( y: Float , x: Float ) -> Float {
6171 return libm_atan2f ( y, x)
6272 }
@@ -126,6 +136,16 @@ extension Double: Real {
126136 return libm_pow ( x, Double ( n) )
127137 }
128138
139+ @_transparent public static func root( _ x: Double , _ n: Int ) -> Double {
140+ guard x >= 0 || n % 2 != 0 else { return . nan }
141+ // Workaround the issue mentioned below for the specific case of n = 3
142+ // where we can fallback on cbrt.
143+ if n == 3 { return libm_cbrt ( x) }
144+ // TODO: this implementation is not quite correct, because either n or
145+ // 1/n may be not be representable as Double.
146+ return Double ( signOf: x, magnitudeOf: libm_pow ( x. magnitude, 1 / Double( n) ) )
147+ }
148+
129149 @_transparent public static func atan2( y: Double , x: Double ) -> Double {
130150 return libm_atan2 ( y, x)
131151 }
@@ -178,6 +198,16 @@ extension Float80: Real {
178198 return libm_powl ( x, Float80 ( n) )
179199 }
180200
201+ @_transparent public static func root( _ x: Float80 , _ n: Int ) -> Float80 {
202+ guard x >= 0 || n % 2 != 0 else { return . nan }
203+ // Workaround the issue mentioned below for the specific case of n = 3
204+ // where we can fallback on cbrt.
205+ if n == 3 { return libm_cbrtl ( x) }
206+ // TODO: this implementation is not quite correct, because either n or
207+ // 1/n may be not be representable as Float80.
208+ return Float80 ( signOf: x, magnitudeOf: libm_powl ( x. magnitude, 1 / Float80( n) ) )
209+ }
210+
181211 @_transparent public static func atan2( y: Float80 , x: Float80 ) -> Float80 {
182212 return libm_atan2l ( y, x)
183213 }
0 commit comments