# Error: PHP math operation

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hello I observed a strange behaviour...

Here is the O/P.. Its working fine till 55-52.5 but when I calculate
55-55 the o/p is -7.105427357601E-15

52.8 = 55-(2.2*1)
50.6 = 55-(2.2*2)
48.4 = 55-(2.2*3)
46.2 = 55-(2.2*4)
44 = 55-(2.2*5)
41.8 = 55-(2.2*6)
39.6 = 55-(2.2*7)
37.4 = 55-(2.2*8)
35.2 = 55-(2.2*9)
33 = 55-(2.2*10)
30.8 = 55-(2.2*11)
28.6 = 55-(2.2*12)
26.4 = 55-(2.2*13)
24.2 = 55-(2.2*14)
22 = 55-(2.2*15)
19.8 = 55-(2.2*16)
17.6 = 55-(2.2*17)
15.4 = 55-(2.2*18)
13.2 = 55-(2.2*19)
11 = 55-(2.2*20)
8.8 = 55-(2.2*21)
6.6 = 55-(2.2*22)
4.4 = 55-(2.2*23)
2.2 = 55-(2.2*24)
-7.105427357601E-15 = 55-(2.2*25)

My Code
------------------------------------------------------------------------------------------------------------
\$total_estimatedhrs = 55;  //from previous calculations
\$estimatedhrs_perday = 2.2; //from previous calculations
\$days_in_sprint = 25; //from previous calculations

for (\$i=1;\$i<=\$days_in_sprint;\$i++) {
\$val = \$total_estimatedhrs-(\$estimatedhrs_perday*\$i);
echo "\$val = \$total_estimatedhrs-(\$estimatedhrs_perday*\$i)<br>";
}
------------------------------------------------------------------------------------------------------------

## Re: Error: PHP math operation

No, you didn't calculate 55-55.  You calculated 55-(2.2*25).
My bet is that 55-55 would come out exact.

Floating-point has roundoff error.  Better get used to it; it's not
going to go away.  An IEEE double has about 15 significant digits
in it.  You got almost 16, so you don't have much ground for
complaint.

## Re: Error: PHP math operation

On Jul 23, 10:28=A0am, gordonb.eu...@burditt.org (Gordon Burditt) wrote:

Hi,

Now what is the solution for this?
When I calculate 55-(2.2*25) I expect the answer to be ZERO.

Vinayak

## Re: Error: PHP math operation

you try to round to "n decimal places where n > 0", as almost none
of those results are exactly representable in binary floating point.

2b.  Explicitly format the result printed to a limited number of
decimal places.

3.  Use something other than binary floating point.  PHP includes
two extensions, bcmath and gmp, which might be more appropriate for

## Re: Error: PHP math operation

wrote:

Rounding errors are a fact of life with floating point arithmetic, for
more details as to why, consult a book on computer science, but it
basically comes down to the fact that there are fractions you can't
represent in base two, just like you can't precisely represent 1/3 in
base 10.

Either learn to live with it, avoid floating point wherever possible,
avoid division wherever possible as it is more likely to yield a float
as a result than the other operations, or use a bignum library which
will mean mathematics will run a lot more slowly and consume far more
memory.

## Re: Error: PHP math operation

wrote:

http://www.php.net/manual/en/language.types.float.php also gives some

## Re: Error: PHP math operation

Vinayak Mutalikdesai wrote:

Binary coded decimal.

Or a better floating point mathematical package than PHP's rubbish one.

Or judicious use of round() and abs() functions..

## Re: Error: PHP math operation

Vinayak Mutalikdesai wrote:

It might help in your understanding if you were to think about how the
numbers are stored in the computer.  The numbers 55 and 55.0 are stored
differently.  The 55 is stored using all 32 bits (one is for the sign of
the number.).  Of course this limits the range of numbers that can be
stored.  The 55.0 is a floating point numbers and is stored differently.
It is stored as 55 in the 24 of the bits (one for the sign) and is
interpreted as 0.55.  The remaining eight bits (one for the sign) is for
the expoonent, which in this case is two.

Now you have 2.2 which is in floating point time 25 which is integer.
The conversion takes place and the result is floating point.  You then
want to subtract this floating point approximation of 2.2 times 25 from
the integer 55.  There is an inevitable roundoff error.

--
Shelly

## Re: Error: PHP math operation

sheldonlg <sheldonlg> wrote:

Your explanation is not actually correct, although you have the right
spirit.  The first part is irrelevant, because 55.0 can be represented
exactly in binary floating point.

The real issue is that 2.2 CANNOT be represented exactly in binary.  It is
an infinitely repeating value, just like 1/3 in decimal.  The value in the
floating point register will be close to 2.2, but not exact.  When you
multiply "close to 2.2" by "exactly 25", what you get is "close to 55.0".
Subtracting that from the "exactly 55" value results in a very small value.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hello All,