codyjimenez Posted August 24, 2012 Share Posted August 24, 2012 This is driving me crazy! Can anyone explain? This echos out FAIL <?php $var1 = 323.4; $var2 = 107.4; $amt1 = $var1+$var2; $amt2 = 430.8; if($amt1==$amt2){ echo "PASS"; }else{ echo "FAIL"; } ?> This along with almost every other combo for var1 and var2 echos out PASS <?php $var1 = 323.3; $var2 = 107.3; $amt1 = $var1+$var2; $amt2 = 430.6; if($amt1==$amt2){ echo "PASS"; }else{ echo "FAIL"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2012 Share Posted August 24, 2012 The problem is due to conversion of the number to binary. Some fractional numbers cannot be represented exactly as a binary number and you end up with a loss of precision. To avoid this conversion error, you need to store and operate on the numbers as BCD (binary code decimals), the same as what a calculator does. See the following bc math extension - http://php.net/manual/en/book.bc.php Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/#findComment-1372122 Share on other sites More sharing options...
codyjimenez Posted August 24, 2012 Author Share Posted August 24, 2012 Thanks. So is no way to get it to work without modifying the actual code and use BCD. My problem now is that we have spent hours and hours writing code without BCD and to go back and put it in will take forever! Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/#findComment-1372135 Share on other sites More sharing options...
MMDE Posted August 24, 2012 Share Posted August 24, 2012 Thanks. So is no way to get it to work without modifying the actual code and use BCD. My problem now is that we have spent hours and hours writing code without BCD and to go back and put it in will take forever! Is there going to be more than one number after the comma (decimal digit whatever you want to call it, 3.4)? If that is never going to happen, and you are sure, you could multiply by ten before doing anything with them. Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/#findComment-1372138 Share on other sites More sharing options...
Psycho Posted August 24, 2012 Share Posted August 24, 2012 My problem now is that we have spent hours and hours writing code without BCD and to go back and put it in will take forever! It shouldn't take that long. Simply do a search for the + symbol in your code. If it is used for adding two number then replace with bcadd. Then do the same for the subtraction symbol and so on. I can't see that it would take that long. Also, since most of the BC function only work on two numbers, if you are adding, subtracting, multiplying multiple numbers in your code you might want to create some functions to handle multiple numbers: function addAll($arrayOfNumbers) { $total = array_shift($arrayOfNumbers); foreach($arrayOfNumbers as $number) { $total = bcadd($total, $number); } return $total; } Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/#findComment-1372141 Share on other sites More sharing options...
Barand Posted August 24, 2012 Share Posted August 24, 2012 Checking for equality in float calculations is always fraught with danger. You need to check that their difference is tolerably small. eg if(abs($amt1-$amt2) < 0.0001){ echo "PASS"; }else{ echo "FAIL"; } Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/#findComment-1372145 Share on other sites More sharing options...
kyle04 Posted August 24, 2012 Share Posted August 24, 2012 $amt1 = $var1+$var2; $amt1 = sprintf ("%01.1f", $amt1); Quote Link to comment https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/#findComment-1372195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.