-2

I just noticed in C#, if I multiply by Math.Pow a double, the result is not correct. Simple test can be done with :

70883187.82 * Math.Pow(10, 2)

Which returns 7088318781.999999

Or same can be done with double c = 70883187.82 * 100

What is the explication behind that ?

Thanks

1 Answers1

0

If you need that level of precision, you should use a decimal instead of a double. A double cannot store an exact value with that level of precision due to the way they are represented in memory.

Each decimal place in a double is calculated as 1/(2^i), so there are many values that cannot be legitimately be represented with a float or double value.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
  • 1
    `70883187.82` is represented in a different number space to `70883187.82M`. A large majority of decimal fractional numbers can't be represented properly by IEEE floating point values. They fall between the rungs of the ladders. The "precision" you talk of only exists because of your decimalized world-view. "Precise for decimal fractions" would be more accurate. – spender Mar 20 '18 at 00:24