0

when i make this multiplication

0.94 * 8700

the output is

8177.999999999999

but it should have been

8178

i'm using java , but i don't think this error is related to a particular Programming language now my question is ... why this happened ??
and what other numbers (just as an example) cause the same error?

dan04
  • 87,747
  • 23
  • 163
  • 198
Ahmad Ibrahem
  • 97
  • 1
  • 6
  • 1
    http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems There's 10 kinds of people in the world : those who understand binary and those who don't. – JiminP Sep 28 '11 at 10:27
  • Here's a pretty good overview of how floating point works: http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html – jncraton Sep 28 '11 at 10:26

2 Answers2

5

The specific reason in your case is that the real number 0.94 cannot be represented exactly in a double precision floating point. When you type 0.94, the actual number stored is 0.939999999999999946709294817992486059665679931640625.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
0

It isn't an error. IEEE floats can't represent decimal numbers exactly.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365