Re: Simple math failing - PHP Bug?

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

 



On Sun, 31 Oct 2004 23:30:53 -0700, Brian T. Allen <brian@xxxxxxxxxxxxxxx> wrote:

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


It would be more than a minor slowdown, as php would have to inplement something that is so slow when done at a far lower level that there used to be a seperate processor just to handle it. Also, 1/3 is certainly basic math, but it eventually gets impossible to store accuratly, as there is not exact decimal value. You could store the fractional value of it, but what happens when you have something like log(sqrt(1/3))? The only way to have an exact value would be to store every single operation done to the number, and rerun them every time the number is refered to, but you still have rounding between every step of that.


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

http://www.smempire.org

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