0

I have strange problem with reading double data in my c++ program, why is c++ adding nines and how can i make him not to?

When i gave him 8.62400e+02 he save it as 862.39999999999997726 not 862.4

enter image description here

Thank you for any help!

1 Answers1

0

You simply cannot express all values exactly in floats or doubles, that's the difference to integers. If you want to get rid of the 9's when printing the value, use an appropriate precision specifier in your printf() format string, this will most likely do the trick, but internally, it won't ever be exactly = 862.4.

Heinz Kessler
  • 1,610
  • 11
  • 24
  • yeah but this makes huge difference in TSP problem, in java my greedy route is 59941.23797294148, while cpp gave me 59892.044729864501278 :/ – apologiessirnoclue Apr 07 '17 at 17:57
  • 1
    Such a huge difference cannot be explained with a songle rounding error. But depending on what your're doing with doubles to get tto this result, rounding errors can add up and you may get such big differences. Math with floating point values really is programming for adults :-) – Heinz Kessler Apr 08 '17 at 05:42
  • The big difference you are seeing in the final result could be either accumulated rounding error, especially if you are adding numbers of very different magnitude, or a logic error in the program. It can be very difficult to tell which. – Patricia Shanahan Apr 08 '17 at 09:46