Substracting floating point value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sathyan
Forum Newbie
Posts: 9
Joined: Thu Aug 06, 2009 12:35 am

Substracting floating point value

Post 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
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: Substracting floating point value

Post by dejvos »

Are you sure that values are equal?
sathyan
Forum Newbie
Posts: 9
Joined: Thu Aug 06, 2009 12:35 am

Re: Substracting floating point value

Post 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.
Last edited by Benjamin on Thu Aug 06, 2009 6:39 am, edited 1 time in total.
Reason: Added [code=php] tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Substracting floating point value

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Substracting floating point value

Post 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;
 
sathyan
Forum Newbie
Posts: 9
Joined: Thu Aug 06, 2009 12:35 am

Re: Substracting floating point value

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Substracting floating point value

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Substracting floating point value

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Substracting floating point value

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
sathyan
Forum Newbie
Posts: 9
Joined: Thu Aug 06, 2009 12:35 am

Re: Substracting floating point value

Post by sathyan »

Thanks baker, onion2k and vladsun for your valid sugestions..........
Post Reply