0

I have never stumbled upon this issue before. What is wrown with the following comparison:

$longest_side = 2.9;
if ($longest_side > 2.9) echo 'Too long'; // Returns true, incorrect

Some debugging:

var_dump($longest_side, 2.9) // Displays float(2.9) float(2.9)

The only way to make it work was to stringify the values

if ((string)$longest_side > (string)2.9) echo 'Too long'; // Returns false, OK

Is this a PHP bug or as constructed?

tim
  • 2,530
  • 3
  • 26
  • 45
  • 3
    Read the [Wikipedia entry about floats](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems) and the [big red warning message in the PHP Docs](http://www.php.net/manual/en/language.types.float.php) about comparing float values – Mark Baker Aug 24 '15 at 09:46
  • 2
    And the correct way to make it work isn't to cast to string, but to compare using an epsilon, as describe in both the above linked pages – Mark Baker Aug 24 '15 at 09:48
  • Thank you for a quick and helpful response. – tim Aug 24 '15 at 09:52

0 Answers0