0

Test 1

if((0.011-0.007) >= 0.004){
    echo 'YES!';
}else{
    echo 'NO!'; 
}

Output: NO! <--- INCORRECT

Test 2

if((0.004) >= 0.004){
    echo 'YES!';
}else{
    echo 'NO!'; 
}

Output: YES!

Test 3

if((0.012-0.008) >= 0.004){
    echo 'YES!';
}else{
    echo 'NO!'; 
}

Output: YES!

Test 4

if((0.011-0.008) >= 0.003){
    echo 'YES!';
}else{
    echo 'NO!'; 
}

Output: NO! <--- INCORRECT

Test 5

if((0.01-0.007) >= 0.003){
    echo 'YES!';
}else{
    echo 'NO!'; 
}

Output: YES!

Test 6

if((0.011-0.007) >= 0.003){
    echo 'YES!';
}else{
    echo 'NO!'; 
}

Output: YES!

Conclusion

Something very strange with the number '0.011'

user6890
  • 484
  • 2
  • 6
  • 16
  • 1
    This is normal. Float point cannot accurately represent some numbers so they just store the approximation of the value. When you do a lot of operation, the precise start to lose as well. – invisal May 15 '16 at 12:31
  • 1
    You should be `$epsilon = 0.001; if((0.011-0.007-0.004) <= $epsilon)`. See [this](http://php.net/manual/en/language.types.float.php) – sjsam May 15 '16 at 12:43
  • 1
    you need to fixed the length of floating point subtraction value. cause if you start compare a float value with another then its okey but if is it with a floating difference then you need to fixed the difference length. See the [online link](https://3v4l.org/OAAOC) – Murad Hasan May 15 '16 at 12:46

0 Answers0