# Innacurate multiplication with decimals

This topic is 5467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I'm having a pickle of a time figuring this one out...I'm using VC++6.0 and I'm using the double data-type. I know for certain that two doubles being multiplied are 1.3 and 4.0 because I use breakpoints to see the values of my variables and I input these into the program anyways... when they are multiplied, insead of getting the expected 5.2 I get 5.1999998092651, which puzzles me greatly...is there some secret math which I have not learned yet or am I going nuts? Thanks for any replies!

##### Share on other sites
floats and doubles can't accurately represent certain numbers because of the way they're stored... it's just something you have to live with. You should expect a certain amount of error with floating-point calculations.

##### Share on other sites
I've read that while searching the web, but these aren't very large number's, only one decimal of precision, I would expect it to get that right...in any case, is there a way around this? I'm trying to reduce fractions and a number like 5.19999801... isn't exactly working terribly well...
Thanks!

##### Share on other sites
Quote:
 It is commonly known that some simple fractions, like 1/3, can not be exactly represented in decimal notation. Depending on the choice of base, there will always be certain fractions that can only be represented by an infinitely repeating series of digits to the right of the decimal point. Because C++ number types use an underlying binary representation, most decimal fractions have no exact representation as standard C++ floating point numbers. For example even the simple decimal number 0.1 (one tenth) translates into the repeating binary fraction, .000110011001100..., ad infinitum. This means that C++ floating point is incapable of exactly representing most decimal fractions, regardless of the choice of precision.
I used to have a link to good article about this, but I can't seem to find it right now. Simply put, the number 0.1 cannot be accurately represented in binary.

##### Share on other sites
crap...i was afraid of that...anyways, time to start looking for a solution, if anyone has one, please let me know.

Thanks Ra!
(and if you do find the article, I'd like to have a read)

##### Share on other sites
1.3 in binary is approximately 1.0100110011001100112. In double form, you can only store so many bits in the mantissa, so it will get truncated after that many bits. When you multiply by 4 (1002), you get 101.001100110011..., which is a teeny bit less than 5.2 (which also cannot be represented exactly in a finite number of bits).

##### Share on other sites
Quote:
 Original post by perfectly_darkcrap...i was afraid of that...anyways, time to start looking for a solution, if anyone has one, please let me know.

The solution is to never depend on exact results from floating-point operations.

Anyway, if the task is to reduce fractions, then I assume you are representing them as a/b, where a and b are integers. Use ints.

##### Share on other sites
How does the Windows calculator do it?

##### Share on other sites
Quote:
 Original post by perfectly_darkI've read that while searching the web, but these aren't very large number's, only one decimal of precision, I would expect it to get that right...in any case, is there a way around this? I'm trying to reduce fractions and a number like 5.19999801... isn't exactly working terribly well...Thanks!

If you're trying to reduce fractions, the solution is: don't actually do any division. Keep the numerator and the denominator as seperate variables (this would be a good place to create a new class, like a Fraction class). To reduce a fraction, you just search for common factors between the numerator and the denominator.

##### Share on other sites
Quote:
 Original post by Anonymous PosterHow does the Windows calculator do it?

By not using builtin types, and instead storing things as character arrays or the like, which then requires them to manually implement their +,-,*,/,etc... operators by hand.

Try googling for BigNum for some library implementations of this (not sure if this will show results for unbounded floating point numbers, but one can hope).

-Mike

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 10
• 9
• 15
• 22