29
de17
5y

> 0.5 + 0.1 == 0.6
> true

> 0.1 + 0.2 == 0.3
> false

🙏🙏

Comments
  • 0
    So JavaScript eats java?
  • 2
  • 10
    Is this another post implying that JS is the only language that hasn't solved the fundamental problem of floats not being able to represent all numbers?
  • 1
    And what is the result with ===
  • 12
    This is not a js problem, but a float problem and every language has it.
  • 0
    @kenogo

    How many decimal places an epsilon value needs to have before coming to an acceptable approximation that two floats are 'equal?'
  • 3
    @monzrmango depends on what you have been doing with the variables because rounding errors can add up. That's a complicated issue in numerics, and it requires thorough analysis of the algorithm.

    But as very basic step, avoid 0.1 stuff with floats and better use 1/2, 1/4, 1/8 and so on, especially for any kind of step size or loop. Or use an integer loop variable, cast it to float and divide it by 10 in every loop run, though that is slower and still inaccurate - it's just that the error at least doesn't add up in the loop.
  • 1
    This is like a recurring topic on devRant 😂

    Again, 0.2 it's periodic in binary, it will *never* be exact in any language/floating point implementation! As well as in math 0.999999999999999999 != 0.9periodic
  • 1
    @kenogo there are no hamsters in India I figured they are fluffy rats so they eat meat
  • 0
    As Car isto Carpet
  • 0
    @JackToolsNet the result is still wrong, but there is almost no situation where you should use == over ===.
  • 0
    @ashishk or car into carcass 😂
  • 0
Add Comment