29
LeMeow
6y

console.log(0.47-0.01===0.46);
Output: false :/

That got me stuck for quite a while..

Learned more about floating point arithmetic and representation 😊

Comments
  • 8
    That's a common issue when you have algorithms that iterate over a floating point interval (integration comes to mind). You either use steps of 1/8, 1/16 etc., that is the fastest solution. Or if you absolutely need 0.1, 0.01 etc. as steps, you take an integer as loop variable, and in the loop, you cast it to float and divide it by 10.0, 100.0 etc.
  • 2
    I did a rant about it once upon a time

    https://devrant.com/rants/1392187/...
  • 7
  • 0
    @FMashiro that is one sick domain
  • 1
    I don't know why people always mention javascript lol almost every language does it
  • 0
    @crisz To summon people like you 😎
  • 1
    @crisz because there's a lot more Javascript developers who don't have the slightest idea about how a computer deals with their programs on a lower level.

    Also because there's a lot more js developers.

    Btw, I'm not saying js developers are less competent or whatever, just that on average, they are less aware of low level stuff.
Add Comment