logoalt Hacker News

zokiertoday at 10:40 AM2 repliesview on HN

That has more to do with decimal <-> binary conversion than arithmetic/comparison. Using hex literals makes it clearer

     0x1.999999999999ap-4 ("0.1")
    +0x1.999999999999ap-4 ("0.1")
    ---------------------
    =0x3.3333333333334p-4 ("0.2")
    +0x1.999999999999ap-4 ("0.1")
    ---------------------
    =0x4.cccccccccccf0p-4 ("0.30000000000000004")
    !=0x4.cccccccccccccp-4 ("0.3")

Replies

jacquesmtoday at 10:51 AM

Absolutely nobody will think this is 'clearer', this is a leaky abstraction and personally I think that the OP is right and == in combination with floating point constants should be limited to '0' and that's it.

show 1 reply
brandmeyertoday at 1:24 PM

Repeating the exercise with something that is exactly representable in floating point like 1/8 instead of 1/10 highlights the difference.