Re: Simple math failing - PHP Bug?

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

 



Chris Shiflett wrote:

--- "Brian T. Allen" <brian@xxxxxxxxxxxxxxx> wrote:


Well, in fairness, it's one of the worst ideas you've ever heard
because you know of this limitation.



Of course. That wasn't a dig at you or anything - just a comment. You're free to heed or ignore it.



In reality, using a calculator or something that doesn't share
this limitation, 0 = 0 and this wouldn't be a problem.



Well, 0 == 0 in PHP, too. Calculators have the same limitations that any other computing device does. Some modern calculators have some pretty impressive precision, but it's not hard to exceed it with a few seemingly harmless operations. A couple of numbers like 12.34567 and 23.45678 can get pretty nasty with a few operations.

Regardless of this, I'm not in favor of depending on anything that's the
least bit questionable or complicated. I don't even like evaluating
integers as boolean unless they are strictly 0 or 1, and even then there
can be problems (you accidentally decrement a variable twice before
evaluating it again, so that it goes from 1 to -1 and continues to
evaluate as true forever).

In short, complexity breeds problems, regardless of how smart you are.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly     HTTP Developer's Handbook - Sams
Coming January 2005         http://httphandbook.org/




No worries, this isn't personal. I just want to explore this limitation and it's implications for my benefit and the benefit of any that read this thread later when they are searching for answers to a similar problem.


I just think the following should work, and outside of this limitation of computers is sound programming:

$subtotal = 0;
$subtotal += 503.54;
$subtotal += 303.55;
$subtotal -= 503.54;
$subtotal -= 303.55;

if($subtotal > 0){
   // A balance is due
}

But if $subtotal is 0.0000000000034322 then it shows a balance is due when one isn't.

When adding dollar amounts using only whole cents (and excluding the limitation we've been discussing) you should always have a credit >= .01, a balance >= .01, or no balance / credit due, which would equal 0 or 0.00, but NOT 0.0000000000034322.

Yes, there are ways to program around this, but it shouldn't be necessary. The example above shouldn't be questionable or complicated, in fact it is about as basic as it comes. It's 2nd Grade math. You just should have to program around that. I would MUCH rather have PHP run a little slower and take up more memory and get the answer right then have every PHP programmer have to program around this limitation for even the most basic math.

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