-2

Something is wrong with eval function

$string = '$val = 1526.65 - 1519.99;';
eval($string);
echo $val; => give 6.6600000000001, expected 6.66

The test is here : http://sandbox.onlinephpfunctions.com/code/fa5b2e7e6602e60cdeb054b705a69e4a226bd52c

Anyone have an explanation ?

Thanh Trung
  • 3,566
  • 3
  • 31
  • 42

1 Answers1

5

This has nothing to do with eval, since the same result is produced by running that line normally. The reason for the strange result is floating point error.

Community
  • 1
  • 1
nkorth
  • 1,684
  • 1
  • 12
  • 28