Unexpected Rounding on Subtraction

G

Guest

H

If you type in say 179.95 then in the next cell 179 and subtract the latter from the former you get 0.95. Actually, you've got 0.949999999999989. Maybe this is for statistical purposes (although this should just apply to relevant functions), but this is a problem, as I want to create a general formula to convert decimal values into degrees, minutes and seconds

PS I know about the formatting solution

Cheers for any hel
 
J

Jerry W. Lewis

0.95 has no exact binary representation (much as 1/3 has no exact
decimal representation). When you enter 179.95, computers (which do
binary math) see not 179.95, but the closest binary approximation that
can fit within the computer's precision.
179.94999999999998863131622783839702606201171875
is the decimal representation of the closest binary approximation in
IEEE double precision (used by Excel, and almost all other hardware and
software). What Excel gives when you subtract 179 is the exact answer
to this approximate problem.

The simplest way to think about these issues is to note that IEEE double
precision can approximate all 15 figure numbers, but only some 16 figure
numbers, hence Help's "specification" that Excel's accuracy is 15
figures. Thus you can think of your problem as

179.950000000000???
-179
-------------------
0.950000000000???

which is consistent with what you got.

0.949999999999989

Integers (<=15 figures) can be exactly represented, but most fractional
values cannot. You will have to either recast your calculations as
strictly integer calculations, or adjust your algorithm to accommodate
15 figure approximations to the numbers that you intend.

In this instance, you input nothing beyond 2 decimal places, and
subtraction cannot result in anything beyond that. Thus
=ROUND(179.95-179,2) will hide the evidence of binary approximations
without violence to the integrity of the calculation. I say "hide" the
evidence, because 0.95 will still be approximated, as revealed by
=0.95-(1-2^-4+2^-7).

Jerry
Excel MVP
Hi

If you type in say 179.95 then in the next cell 179 and subtract the
latter from the former you get 0.95. Actually, you've got
0.949999999999989. Maybe this is for statistical purposes (although
this should just apply to relevant functions), but this is a problem,
 
A

AlfD

Hi!

In the example you quote, the difference between the true number an
the computed number is about 4*10^-11 (4E-11) seconds if the origina
numbers are degrees. Something like the angle subtended at the eart
by a line 3 millionths of an inch long drawn on the surface of th
moon.

Unfortunately, I think you will find Excel won't do better. If you nee
greater accuracy, then you need something working to more than Excel'
15 d.p. Even then, decimal fractions will rarely equal binar
fractions, so there will still be "errors".

Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top