Stream: beginners

Topic: Testing for -0.0 or +0.0


view this post on Zulip Aurélien Geron (Sep 21 2026 at 04:53):

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

view this post on Zulip Richard Feldman (Sep 21 2026 at 12:10):

how do other languages do this?

view this post on Zulip Richard Feldman (Sep 21 2026 at 12:11):

I've actually never needed to test for this :sweat_smile:

view this post on Zulip Brian Teague (Sep 21 2026 at 20:31):

My initial guess is just right shift to check the sign bit
is_negative = |x| (x >> 63) != 0

view this post on Zulip Aurélien Geron (Sep 21 2026 at 23:16):

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:

view this post on Zulip Aurélien Geron (Sep 21 2026 at 23:18):

I 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?

view this post on Zulip Notification Bot (Sep 22 2026 at 02:11):

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