I have seen posts about `0.0 == -0.0` but I wasn’t...
# announcements
g
I have seen posts about
0.0 == -0.0
but I wasn’t expecting to see that in a multiplication 🤔
m
that's how floats work 😄
r
@Marko Mitic I think you mean that's how floats don't work 🧌
m
float has a dedicated sign bit, so -0 is a valid option
b
the first bit of each float denotes plus/minus. Therefore float have both
0.0
and
-0.0
but they are equal.
m
ints only have one 0, but
-Int.MIN_VALUE == Int.MIN_VALUE
r
Yes, I know, though that doesn't mean much. Look how many different representations there are for
NaN
. They could have just added that one to the list.
Yeah, two's compliment was a surprisingly brilliant idea, despite the edge cases.
m
edge cases are overflow-related, there isn't a correct way to handle those so I can't take that as a negative
so, two's compliment was a surprisingly brilliant idea , despite the edge cases
r
Fair point 🙂
d
It's great how Float.NaN != Float.NaN
It's great that
(x != x)
is how you identify NaN
This is amazing api design
Violating equals contract
Yes
r
😆
g
I guess sign of your 0.0 is another thing you cant choose lol
How do you flip sign of a double or float in this case? Apparently unary minus doesn't work
d
You could do it on the raw bits
a
-0.0 is an IEEE standard btw.
g
Yes, I’m aware of that. This isn’t the first time I saw -0.0. But in the articles I saw in the past they mostly talked about its existence and what it returns when compared to positive zero. I wasn’t expecting to see it as a result of simple multiplication like this. I did a quick research and I couldn’t find any posts related to this. I’ve seen posts about why one should use BigDecimal over double, and how it doesn’t handle precision well, but nothing related to this. And I think this is important, or I am missing something