Multiplying two doubles

T

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.
 
M

mlimber

Tenacious said:
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
 
A

Alf P. Steinbach

* 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.
 
N

Noah Roberts

Tenacious said:
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.
 
L

Luke Meyers

Noah said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top