On 30 Jun 2004 13:31:26 -0700,
ma*****@gmail.com (Mark) wrote:
See something weird on this result?
632.35 - 632.35 = -1.13686837722E-013
If anyone has seen something similar, please let me know!
this a part of the code:
$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );
I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.
any ideas??
This is normal behaviour from computers which use the IEEE floating-point
format (i.e. pretty much all of them). Many decimal numbers have no exact
representation in this format, however they will differ only by a very small
value. So when you start doing calculations, small errors build up.
It looks like you are working with monetary values. You should avoid using
floating point numbers for money, and convert everything to integer values of
the smallest currency unit or fraction thereof you want to deal with, for
instance cents or pence. Then only convert to a floating point value for
display. This way all calculations remain exact.
--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk /
http://www.andyhsoftware.co.uk/space