# Multiplying two doubles

Discussion in 'C++' started by Tenacious, Jun 2, 2006.

1. ### TenaciousGuest

Why is it that when I multiply two double values like this:

double Multi = 100.0;
double Force = 0.29;
double Result = Multi * Force;

I get Result = 28.999999999996 instead of 29.0? I'm not an expert
mathematician, but 0.29 * 100 has always been 29 as far as I can
remember. If anyone can help me understand this, I would appreciate it
very much.

Tenacious, Jun 2, 2006

2. ### mlimberGuest

Tenacious wrote:
> Why is it that when I multiply two double values like this:
>
> double Multi = 100.0;
> double Force = 0.29;
> double Result = Multi * Force;
>
> I get Result = 28.999999999996 instead of 29.0? I'm not an expert
> mathematician, but 0.29 * 100 has always been 29 as far as I can
> remember. If anyone can help me understand this, I would appreciate it
> very much.

See this FAQ and the ones following:

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16

Cheers! --M

mlimber, Jun 2, 2006

3. ### Alf P. SteinbachGuest

* Tenacious:
> Why is it that when I multiply two double values like this:
>
> double Multi = 100.0;
> double Force = 0.29;
> double Result = Multi * Force;
>
> I get Result = 28.999999999996 instead of 29.0? I'm not an expert
> mathematician, but 0.29 * 100 has always been 29 as far as I can
> remember. If anyone can help me understand this, I would appreciate it
> very much.

A 'double' value is represented using some number of bits, typically 64.
There are 2^64 different patterns of bit-values of length 64, and
hence, for a 64-bit 'double', at most 2^64 possible different 'double'
values can exist. Typically these possible values are spread over a
fairly large range, say about 5*10^-324 up to 1.7*10^308, plus an exact
0; the distance between two adjacent 'double' values is very small for
very small numbers, and very large for very large numbers.

Which means that /most/ exact number values you can think of simply
cannot be represented exactly as 'double' values: such a value is
instead represented by the closest 'double' value.

Due to the way 'double' values are typically represented, typically
small integers /can/ be represented exactly. So you can assume that
your number 100.0 is represented exactly since it's a small integer,
while 0.29 is represented by some closest possible 'double' value which
is just slightly less than or more than 0.29. In fact, it seems that
you have established that that value, for your C++ implementation,
corresponds to roughly 0.28999999999996.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Jun 2, 2006
4. ### Noah RobertsGuest

Tenacious wrote:
> Why is it that when I multiply two double values like this:
>
> double Multi = 100.0;
> double Force = 0.29;
> double Result = Multi * Force;
>
> I get Result = 28.999999999996 instead of 29.0? I'm not an expert
> mathematician, but 0.29 * 100 has always been 29 as far as I can
> remember. If anyone can help me understand this, I would appreciate it
> very much.

Computers, as they exist today, are incapable of performing floating
point operations in a 100% accurate way. This is why you don't test
for equality with == and why you can't bet on .29 * 100 being 29. In
fact if you put such a number into an integer the most likely number
you would get in your case is 28...which is just wrong.

This is something you must never forget as a computer programmer.

Noah Roberts, Jun 2, 2006
5. ### Luke MeyersGuest

Noah Roberts wrote:
> Tenacious wrote:
> > Why is it that when I multiply two double values like this:
> >
> > double Multi = 100.0;
> > double Force = 0.29;
> > double Result = Multi * Force;
> >
> > I get Result = 28.999999999996 instead of 29.0? I'm not an expert
> > mathematician, but 0.29 * 100 has always been 29 as far as I can
> > remember. If anyone can help me understand this, I would appreciate it
> > very much.

>
> Computers, as they exist today, are incapable of performing floating
> point operations in a 100% accurate way. This is why you don't test
> for equality with == and why you can't bet on .29 * 100 being 29. In
> fact if you put such a number into an integer the most likely number
> you would get in your case is 28...which is just wrong.

It's worth noting that while what you say is true, it would be
perfectly possible to implement, on any modern computer, a
floating-point data type which would perform the above calculation, and
any other multiplications (etc.) involving rational numbers, perfectly
and without rounding. The reason we don't do that is efficiency -- a
robust rational number data type would be more expensive in terms of
time and space than the approximation provided by double and float.
Part of this is inherent to the representation, and part is because
your computer (probably) has a floating-point math coprocessor
specifically designed for float/double math.

Luke

Luke Meyers, Jun 3, 2006