OiO.lk Blog PHP Is floating-point math broken?
PHP

Is floating-point math broken?


I am maintaining a legacy application, and I have this logical comparison:

$return = eval('return (361.32 < (327.41 + 33.91))'); // 327.41 + 33.91 = 361.32
var_dump($return); // bool(true)

Online running

The values are equal, so the result should be false.

I tried these ways but all them are returning true.

eval('return (361.32 < (327.41 + 33.91) ? true : false;)');
eval('return (bool) (361.32 < (327.41 + 33.91));')
eval('return 361.32 < (327.41 + 33.91);')

The ideal scenario is not to use eval(), but as it is a legacy application, I can’t do this at this moment.

Can someone help me to solve this behavior?



You need to sign in to view this answers

Exit mobile version