0

I'm creating a program that does a large number of very specific calculations and currently facing the following situation:

I have a number: 1758.45 that is an initial input to a large calculation. After computing the output I receive an incorrect number. I noticed that calculations were very slightly off (0.000000000000000000001 instead of 0.000000000000000000002) and eventually I traced the miscalculation back to the number above (1758.45).

Now the problem that I'm having is that when I print the above number I get: enter image description here

Whereas the number should be just 1758.45.

I've initialized it as: double x = 1758.45;

And I'm printing it out via:

#include <iostream>
std::cout.precision(75);
std::cout << (x) << std::endl;

Are these numbers the cause of my miscalculation? Or is it just a matter of how I'm printing the number out (x is not really stored with the additional values)?

Paul Warnick
  • 903
  • 2
  • 13
  • 26
  • Welcome to the world of floating point math. May you enjoy your stay. – tadman May 31 '16 at 19:05
  • 1
    Mandatory read http://floating-point-gui.de/ – Basile Starynkevitch May 31 '16 at 19:06
  • 1
    Unless your platform is using something different, it is completely unreasonable to expect 75 digits of precision. An IEEE-754 four byte float will have at best 7.5 significant decimal digits and an IEEE-754 eight byte float will have at, at best, 15.5 decimal digits. – Jon Trauntvein May 31 '16 at 19:10
  • @JonTrauntvein Ah I see, that makes sense but how do people go about computing large calculations? For example part of my calculation runs in a loop and if the numbers are even slightly off the errors become significant quickly. – Paul Warnick May 31 '16 at 19:33
  • 1
    If you want accuracy use an integer type, one that will give you enough significant figures. Multiply by some factor such that truncation won't cause errors when you divide by that factor to return to a floating point type. – Ian May 31 '16 at 19:39
  • @Ian I'm not sure if I fully understand how you would like me to approach this. Can you give an example of using integers instead? – Paul Warnick May 31 '16 at 19:44
  • 1
    I had a problem with existing code recently that stored money amounts in doubles. Mostly it worked but sometimes, for example when comparing a total with zero it failed because the total was something like 0.0000000001. The solution was already within the application. There was a currency class that multiplied double amounts by 10,000 and held them in integer types. All calculations were done with these integers. Whenever an amount was displayed it was divided by 10,000 and rounded to the nearest penny. – Ian May 31 '16 at 19:49
  • That's a similar situation to the one I'm facing. Thank you, hopefully this will work. – Paul Warnick May 31 '16 at 19:54
  • Try testing the 'sizeof(long double)' on your system to see if it is greater than the 'sizeof(double)' if so you may be able to achieve greater precision in your computations, keep in mind that your cpu may not have an fpu that can handle floating point calculations greater than 64 but in hardware which could slow down your program significantly – Alex Zywicki May 31 '16 at 20:40

0 Answers0