Re: PHP can't compare...

From: Date: Tue, 04 Dec 2001 22:05:04 +0000
Subject: Re: PHP can't compare...
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-72518@lists.php.net to get a copy of this message
Hello, this is how floating point numbers work. 3.55 as float is not 'exactly' 3.55, but it is stored in memory as an as good as possible representation, something like 3.55000000001 perhaps. If you simply compare this: 3.55000000000001 + 60.12000000000000001 != 63.66666666666669 so that's why the comparison fails. Derick On Tue, 4 Dec 2001, Matthew Hagerty wrote: > Greetings, > > Explain to me why the first condition fails! > > <?php > > $fFloat1 = 3.55 + 60.12; > $fFloat2 = 63.67; > > if ( $fFloat1 == $fFloat2 ) > print "Equal"; > else > print "Not Equal"; > > settype($fFloat1, "string"); > settype($fFloat1, "double"); > > if ( $fFloat1 == $fFloat2 ) > print "Equal"; > else > print "Not Equal"; > > ?> > > Thanks, > Matthew > > > > -- > PHP Development Mailing List <http://www.php.net/> > To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net > For additional commands, e-mail: php-dev-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.dev (#72518) next »