0

Please check below code.

$value1= 57.6;
$value2 = 177.6000 - 120.0000;
echo $value1.' '.$value2."<br/>";
var_dump($value1);
echo "<br/>";
var_dump($value2);
echo "<br/>";
var_dump($value1==$value2);
echo "<br/>";
var_dump($value1>$value2);
echo "<br/>";
var_dump(57.6 > (177.6000 - 120.0000));

If you check there is no difference in $value1 and $value2 but when i am using $value1>$value2 it's satisfying the condition which shouldn't happen. Below is output

57.6 57.6
float(57.6)
float(57.6)
bool(false)
bool(true)
bool(true)
Karan Adhikari
  • 485
  • 1
  • 5
  • 16

1 Answers1

1

PHP (like most programming languages, spreadsheets, etc) uses IEEE floating point numbers, which often can't represent decimal numbers exactly

If you subtract $value1-$value2 you'll see that the result is tiny but not zero

In most cases, the advantages of the approximation outweigh the disadvantages; every now and then, though, the inaccuracies add up and cause real problems

The most common caveat is that == is pretty much useless for floats; instead, subtract the two values and check that the (absolute value of the) result is suitably small (less than some value, often vaguely called "epsilon")

Jiří Baum
  • 6,697
  • 2
  • 17
  • 17