How do you test whether an F64 value is -0.0 or +0.0?
Here are a few failed attempts:
» x = -0.F64
assigned `x`
» x == 0.F64
True
» x == -0.F64
True
» x < 0
False
» match x {
… 0 => "zero"
… -0 => "-zero"
… _ => "other"
… }
"zero"
» x.is_negative()
False
And here are a few successful ones, but they're not great:
» 1 / x < 0
True
» Str.inspect(x) == "-0"
True
» x.to_bits() == 0x8000000000000000
True
how do other languages do this?
I've actually never needed to test for this :sweat_smile:
My initial guess is just right shift to check the sign bit
is_negative = |x| (x >> 63) != 0
Richard Feldman said:
I've actually never needed to test for this :sweat_smile:
Yeah, me neither, I just wondered how one might implement F64.atan2 with the IEEE 754 edge cases for ±0.0.
Richard Feldman said:
how do other languages do this?
Apparently different languages have different solutions:
x == 0.0 && signbit(x)Object.is(x, -0)isNegativeZero xx.floatingPointClass == .negativeZero or x.isZero && x.sign == .minusx.equals(-0.0)x == 0.0 and math.copysign(1.0, x) == -1.0x == 0.0 && (1.0 / x) == -Float::INFINITYI feel like Haskell's solution is the best: explicit, readable, efficient, avoids user questions, errors and inefficient hacks. Maybe we should add F64.is_negative_zero and F32.is_negative_zero?
11 messages were moved from this topic to #ideas > Add atan2 to the stdlib by Luke Boswell.
Last updated: Sep 24 2026 at 15:59 UTC