Jump to content

Recommended Posts

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";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/267528-simple-if-statement-in-not-working/
Share on other sites

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

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.

 

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;
}

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";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.