RE: [PHP] arithmetic operation inconsistent

From: Date: Thu, 28 Feb 2002 14:35:09 +0000
Subject: RE: [PHP] arithmetic operation inconsistent
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-86603@lists.php.net to get a copy of this message
Hmmmmm...you may be on to something. If you run this code you will see that about 30% of the time $ns1 IS actually 1. the funny thing is that if you remove the doubleval calls, $ns1 purports to have a value > than 1. Interesting... <?PHP srand ((double) microtime() * 1000000); #### Income statement $ns1 = doubleval(number_format(rand(900,1200)*1000)) ; $cogs1 = doubleval(number_format(rand(450,600)*1000)) ; $gm1 = doubleval(number_format (($ns1 - $cogs1) * 1000)); echo '1:'.$ns1."\n"; echo '2:'.$cogs1."\n"; echo '3:'.$gm1."\n"; ?> * * Cal Evans * Journeyman Programmer * Techno-Mage * http://www.calevans.com * -----Original Message----- From: Roy Cabaniss [mailto:rcaban@cabanisspc.uamont.edu] Sent: Thursday, February 28, 2002 8:17 AM To: php-general@lists.php.net Subject: [PHP] arithmatic operation inconsistant I have a very simple php page which I am creating an accounting tutorial from. I am writing all of the equations on an include file and then calling the file at the beginning of the appropriate pages My problems lie in inconsistant arithmatic operations. For some reason the equation for $gm1 does not always act as though $ns1 has a value. Only about 30% of the time does $gm1 give the number I expect to see. The rest of the time what I get is a negative (-$cogs1 +1). As if you had taken 1 and subtracted $cogs1. All help MUCH appreciated. srand ((double) microtime() * 1000000); #### Income statement $ns1=number_format(rand(900,1200)*1000) ; $cogs1=number_format(rand(450,600)*1000) ; $gm1=number_format (($ns1 - $cogs1) * 1000); Dr. Roy F. Cabaniss Associate Professor of Business University of Arkansas Monticello http://cabanisspc.uamont.edu/~rcaban -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#86603) next »