Re: Simple math failing - PHP Bug?

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

 



Thomas Goyne wrote:

On Sun, 31 Oct 2004 21:52:32 -0700, Brian T. Allen <brian@xxxxxxxxxxxxxxx> wrote:

Hi,

OK, I'm totally stumped by this. This should be the simplest math imaginable (addition and subtraction), but PHP is coming up with the wrong answer!

[snip]
Is this legitimately bad math on the part of PHP?

Thanks,
Brian


This is a flaw in how computers handle floating point numbers, not php. As the memory needed to store exact floats to any useful precision would quickly get out of control, floats are handled inprecisely. As a result, you should never test for equality with a float, but rather check if the float is within a certain range of the desired number.



Wow, this is pretty dissappointing.

I can understand if I was doing math on 12 or 13 digit numbers, but just adding and subtracting back out a couple of 2 digit decimals I would expect to end up back at zero.

I understand it isn't PHP's fault.

Thanks for the quick reply,
Brian

--
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