Re: Weird results of "=="

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jasper Bryant-Greene wrote:


Am I missing something really obvious here?


This is an inherent problem with floating-point operations, especially comparison, and is not unique to PHP. Often numbers will be off by some miniscule amount, just enough to make them not equal.

What I would do in this situation is create a function float_equals(), after deciding what delta is acceptable for your situation, like this:

define('MAX_FLOAT_DELTA', 0.001); // Or whatever is acceptable for you

function float_equals($a, $b) {
    return (abs($a - $b) <= MAX_FLOAT_DELTA);
}

Then use float_equals($x, $y) instead of $x == $y.

Jasper

Couldn't have said it better!

Evert

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux