https://kotlinlang.org logo
Title
g

goku

08/27/2019, 1:32 PM
I have seen posts about
0.0 == -0.0
but I wasn’t expecting to see that in a multiplication 🤔
m

Marko Mitic

08/27/2019, 1:34 PM
that's how floats work 😄
r

Ruckus

08/27/2019, 1:35 PM
@Marko Mitic I think you mean that's how floats don't work :troll:
m

Marko Mitic

08/27/2019, 1:37 PM
float has a dedicated sign bit, so -0 is a valid option
b

Burkhard

08/27/2019, 1:37 PM
the first bit of each float denotes plus/minus. Therefore float have both
0.0
and
-0.0
but they are equal.
m

Marko Mitic

08/27/2019, 1:38 PM
ints only have one 0, but
-Int.MIN_VALUE == Int.MIN_VALUE
r

Ruckus

08/27/2019, 1:38 PM
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

Marko Mitic

08/27/2019, 1:44 PM
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

Ruckus

08/27/2019, 1:47 PM
Fair point 🙂
d

Dico

08/27/2019, 5:03 PM
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

Ruckus

08/27/2019, 5:14 PM
😆
g

goku

08/27/2019, 11:11 PM
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

Dico

08/27/2019, 11:32 PM
You could do it on the raw bits
a

Al Warren

08/27/2019, 11:49 PM
-0.0 is an IEEE standard btw.
g

goku

08/28/2019, 12:02 AM
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