Page 1 of 1
Substracting floating point value
Posted: Thu Aug 06, 2009 12:55 am
by sathyan
I have two floating point variables containing values 4048.583 each . When i try to substract one from another i expect to get the result as zero but i am getting the result as -4.54747350886E-13
Can Any body solve my problem..
Thanx in Advance.
Sathyan
Re: Substracting floating point value
Posted: Thu Aug 06, 2009 2:35 am
by dejvos
Are you sure that values are equal?
Re: Substracting floating point value
Posted: Thu Aug 06, 2009 4:51 am
by sathyan
Following is the code I used .
Code: Select all
<?
$hector=0;
$are=40;
$sqrmetr=48.583;
$bal1=0;
$bal2=67;
$bal3=20.6478;
$resH=0;
$resA=26;
$resSq=72.0648;
$landAssignSqrMtr=$hector*10000+$are*100+$sqrmetr;
$totalBalanceSqrMtr=$bal1*10000+$bal2*100+$bal3;
$totalReserve=$resH*10000+$resA*100+$resSq;
$balToAssign=$totalBalanceSqrMtr-$totalReserve;
$assignPossible=$balToAssign-$landAssignSqrMtr."<br>";
?>
After executing this code I am getting value to $assignPossibe as -4.54747350886E-13 instead of expected zero.
Please go through the code and suggest me a solution
Thanks in Advance.
Sathyan.
Re: Substracting floating point value
Posted: Thu Aug 06, 2009 5:16 am
by Mark Baker
quoting directly from the PHP manual:
Warning
Floating point precision
It is typical that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9.
This is due to the fact that it is impossible to express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3.
So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.
The same applies to pretty much any language running on a digital computer.
You may find the following code snippets from
comments in the manual useful if you need an absolute degree of precision
Code: Select all
function floatcmp($f1,$f2,$precision = 10) // are 2 floats equal
{
$e = pow(10,$precision);
$i1 = intval($f1 * $e);
$i2 = intval($f2 * $e);
return ($i1 == $i2);
}
function floatgtr($big,$small,$precision = 10) // is one float bigger than another
{
$e = pow(10,$precision);
$ibig = intval($big * $e);
$ismall = intval($small * $e);
return ($ibig > $ismall);
}
function floatgtre($big,$small,$precision = 10) // is on float bigger or equal to another
{
$e = pow(10,$precision);
$ibig = intval($big * $e);
$ismall = intval($small * $e);
return ($ibig >= $ismall);
}
Otherwise just echo your output rounded to an acceptable degree of precision
Re: Substracting floating point value
Posted: Thu Aug 06, 2009 6:42 am
by Benjamin
This works fine on 5.2.6. Outputs 0.
Code: Select all
<?php
$x = (float) 4048.583;
$y = (float) 4048.583;
$b = $x - $y;
echo $b;
Re: Substracting floating point value
Posted: Fri Aug 07, 2009 1:17 am
by sathyan
When you assign the values(4048.583) directly to the variables its working . But I am geting the values from the result of a calculation as given in the code above .........
Still waiting for an exact solution to my problem....
Thanx in Advance...
Sathyan
Re: Substracting floating point value
Posted: Fri Aug 07, 2009 2:51 am
by Mark Baker
sathyan wrote:Still waiting for an exact solution to my problem....
What solution do you want? I've already pointed you to several: rounding, using the arbitrary precision math functions - what's wrong with them?
Re: Substracting floating point value
Posted: Fri Aug 07, 2009 3:09 am
by onion2k
sathyan wrote:Still waiting for an exact solution to my problem....
This forum is for people to help one another voluntarily. It is not here for people to give you "an exact solution". If that's what you're after I suggest you pay someone to write the code for you.
Re: Substracting floating point value
Posted: Fri Aug 07, 2009 3:43 am
by VladSun
When you check floats against a zero value you should do this by comparing the absolute value of the float to a very small number (you choose it) instead of a zero value.
This way the result you get (4.54747350886E-13) can be considered a "zero value" if you choose a "zero threshold value" of 1.0E-10 for example.
Re: Substracting floating point value
Posted: Fri Aug 07, 2009 5:23 am
by sathyan
Thanks baker, onion2k and vladsun for your valid sugestions..........