apple / swift-numerics

Advanced mathematical types and functions for Swift
Apache License 2.0
1.67k stars 142 forks source link

Floating point exponent `pow` returns NaN for integer exponents #221

Closed PatrickPijnappel closed 2 years ago

PatrickPijnappel commented 2 years ago

Feel free to close if this is intentional, but currently the floating-point version of pow returns NaN for e.g. Double.pow(-1, 2.0), even though that's not complex. Shouldn't we be checking for Int(exactly: y) != nil? This appears to be the behavior of Darwin.pow(-1, 2.0).

public static func pow(_ x: Double, _ y: Double) -> Double {
    guard x >= 0 else { return .nan }
    return libm_pow(x, y)
}
xwu commented 2 years ago

This behavior is deliberate. See these links:

PatrickPijnappel commented 2 years ago

@xwu Ah great that's what I was looking for. Makes sense, thank you.