-3

Possible Duplicate:
Is JavaScript's Math broken?

I have fount weird problem in arithmetic precision in OCaml. Look:

# 1.1+.2.2;;
 - : float = 3.30000000000000027

It happen only when I do: something.1 + something.2. Could someone try it?

Community
  • 1
  • 1
zie1ony
  • 1,190
  • 2
  • 14
  • 33
  • 9
    That's not weird, that's intrinsic to floating point numbers. You're the 1000th (ballpark) user to stumble upon this. –  Dec 01 '11 at 21:17
  • 10
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Dave Rager Dec 01 '11 at 21:19

2 Answers2

5

This is a reality of using floating-point values. While 1.1 is represented exactly in decimal with two digits, it takes an infinite number of binary digits to represent this value. Since you are are storing 1.1 in binary with a finite number of bits, rounding error occurs.

David Alber
  • 17,624
  • 6
  • 65
  • 71
1

Ocaml float numbers are IEEE754 double precision numbers. You'll have the same behavior in other programming languages. Perhaps you want floating bignums (which Ocaml don't provide).

A computer has always finite precision native numbers (much like a calculator).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547