Re: Floored!

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

 



On Thu, 2006-02-23 at 23:38 -0700, Ben Miller wrote:
> If anyone has an answer for this, I would be greatly appreciative.
> 
> I was trying to compare two values, the first is the total cost of products
> to be refunded, and the second is the original order total.  To the naked
> eye, they both were coming out as 102.85, yet an if($Refund_Amount >
> $Order_Total) was coming back true, so.....
> 
> I ran some tests, and did the following:
> 
> 		$Order_Total = sprintf("%01.20f",$Order_Total);
> 		$Refund_Amount = sprintf("%01.20f",$Refund_Amount);
> 
> which produced:
> 
> 		$Order_Total = 102.84999999999999431566
> and		$Refund_Amount = 102.85000000000000852651
> 
> so, I figured I would try the following to make sure that the figures in the
> database weren't weird:
> 
> 		$Bar = 102.85;
> 		$Foo = sprintf("%01.20f",$Bar);
> 
> 		echo "\$Foo = $Foo";
> 
> which produced:
> 
> 		$Foo = 102.84999999999999431566;
> 
> I am completely lost.
> 

This is an oversimplification but basically to represent any decimal in
binary you need to do so as the sum of the reciprocal powers of two.
That is, .5 + .25 + .125 + .0625... an so on.  If you have nice even
numbers like .75 then a few bits will do.  If you don't you need
increasingly smaller terms until you reach the sum.  You need 37 bits
just to represent .85 exactly.  The floating point numbers your are
using only have 32 bits total (and not even all of them are mantissa
bits).


A description of the standard:

http://www.psc.edu/general/software/packages/ieee/ieee.html


An excellent treatise of the subject:

http://docs.sun.com/source/806-3568/ncg_goldberg.html


Brandon

-- 
Brandon Enright
UCSD ACS/Network Operations
bmenrigh@xxxxxxxx

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