Substracting floating point value
Moderator: General Moderators
Substracting floating point value
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
Can Any body solve my problem..
Thanx in Advance.
Sathyan
Re: Substracting floating point value
Are you sure that values are equal?
Re: Substracting floating point value
Following is the code I used .
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.
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>";
?>
Please go through the code and suggest me a solution
Thanks in Advance.
Sathyan.
Last edited by Benjamin on Thu Aug 06, 2009 6:39 am, edited 1 time in total.
Reason: Added [code=php] tags.
Reason: Added [code=php] tags.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Substracting floating point value
quoting directly from the PHP manual:
You may find the following code snippets from comments in the manual useful if you need an absolute degree of precision
Otherwise just echo your output rounded to an acceptable degree of precision
The same applies to pretty much any language running on a digital computer.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.
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);
}
Re: Substracting floating point value
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
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
Still waiting for an exact solution to my problem....
Thanx in Advance...
Sathyan
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Substracting floating point value
What solution do you want? I've already pointed you to several: rounding, using the arbitrary precision math functions - what's wrong with them?sathyan wrote:Still waiting for an exact solution to my problem....
Re: Substracting floating point value
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.sathyan wrote:Still waiting for an exact solution to my problem....
Re: Substracting floating point value
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.
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.
There are 10 types of people in this world, those who understand binary and those who don't
Re: Substracting floating point value
Thanks baker, onion2k and vladsun for your valid sugestions..........