0

I am trying to sum this:

2449210.0000000000 + 0.00010000000000000000

and I get this:

2449210.0000999998

both numbers are defined as double and I am programming in C using VS10.

I have been reading about precission in this field but I have come to no profitable solution (something with long double??). I need this sumation to be correct. Any ideas?

EDIT: I dont want to know why this happens (already do), but how to run around this deficiency.

thanks

  • The decimal number `0.0001` cannot be precisely represented by `double` or even by `long double` in the same way that `1 / 7` cannot be precisely represented in decimal notation. – Weather Vane Sep 28 '16 at 10:05
  • Here is [another duplicate](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Weather Vane Sep 28 '16 at 10:10
  • @WeatherVane thanks but my question´s aim is not to understand what happens (I already did) but to obtain a solution that could help me handle this deficiency... any ideas? separate the number in two ints (one for the integer and other for the decimal)? I need this to work accurately. – Pelayo Peñarroya Sep 28 '16 at 10:15
  • 1
    You could use fixed-point arithmetic, for example 64 bit `int` can represent about 19 decimal digits. But you'll still have the same problem with other numbers such as `1/7` or `1/3` and so on. You simply cannot represent an infinite possibility of numbers in a finite space. You have to put up with approximations. – Weather Vane Sep 28 '16 at 10:19
  • If you want exact representation of terminating decimal fractions, and exact addition, consider a decimal arithmetic library. If you need exact representation of all rational numbers look for a rational arithmetic library. Use `double` when you want very efficient, very close approximations to a wide range of numbers. – Patricia Shanahan Sep 28 '16 at 22:48

0 Answers0