RE: [PHP] math problem

From: Date: Sun, 13 Apr 2003 21:00:41 +0000
Subject: RE: [PHP] math problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-143694@lists.php.net to get a copy of this message
> When I supply the script below with these values: > $Age = 23, $Loan = 5000, $Salary = 25000 > I don't get the expected result: "YES, we are etc." > Could someone please explain why? > Thanks a lot, Bastiaan > > You can double check the script at www.dreyershofje.nl > > > <?php > $SalaryAllowance = $Salary/5; > $AgeAllowance = ($Age/10 - ($Age%10)/10)-1; > $LoanAllowance = $SalaryAllowance * $AgeAllowance; > echo "Loan wanted: $Loan"; > echo "Loan amount we will allow: $LoanAllowance"; > if ($Loan <= $LoanAllowance) echo "YES, we are delighted to accept your > application"; > if ($Loan > $LoanAllowance) echo "Sorry, we cannot accept your > application > at this time"; > ?> Probably a floating point error problem. If you store 5000.00 into a variable, since it's a floating point number, it's perfectly acceptable to store 4999.99999999 into the field (with the appropriate amount of significant digits). So if you turned right around and compared that to an integer 5000, it's not going to be equal. You could probably solve your problem by casting $LoanAllowance to an integer (you're not going to lend cents, are you?) and then do the compariston. $LoanAllowance = (int)$LoanAllowance ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/

« previous php.general (#143694) next »