Why 6.84 - 3.6 == 3.2399999999999998

前端 未结 7 666
星月不相逢
星月不相逢 2020-12-22 03:13

I just encountered this and can\'t figure out why exactly Ruby behaves this way.

Could someone explain why in Ruby:

6.84 - 3.6 == 3.2399999999999998
         


        
相关标签:
7条回答
  • 2020-12-22 03:47

    Float/Double is not that precise in most cases (because floats interpolate numbers with base2). The result of floating point operations may vary on different CPU's. The area close to .01 is more precise than the area close to .99

    If you need to compare values calculated with float operations use an epsilon value (really small value) in order to erase rounding errors.

    Pseudocode:

    boolean equals(float x, float y) {
    float result = x - y;
    if(result*result < epsilon)
      return true;
    else 
      false;
    }  
    
    0 讨论(0)
提交回复
热议问题