[thelist] PHP type and value match, but value comparssion fails.

sbeam sbeam at syxyz.net
Wed Aug 25 08:51:08 CDT 2004

On Wednesday 25 August 2004 09:03 am, Liam Delahunty wrote:
> eg,
>   3 units X 0.5 = 1.5, works
>   3 units X 0.6 = 1.8, fails...
> So, what gives? How can I check these values, and what is causing the
> errors?
read the box about "Floating point precision" where it says "never 
compare floating point numbers for equality" 

This is all true of *any* computer language, its a limitation of trying 
to represent arbitrarily precise numbers with a finite number of 1's 
and 0's. 

for currencies, you need to do all the math with integers and just 
divide by 100 at display/storage time, I like "sprintf('%.2d', 
$total/100)" myself.


# S Beam - Web App Dev Servs
# http://www.onsetcorps.net/

More information about the thelist mailing list