0

Or rather, why does (= 1e16 (- 1e16 1)) return true? How can I receive more accurate answer?

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
vxs8122
  • 834
  • 2
  • 10
  • 22
  • 2
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – sds Mar 08 '15 at 00:05

1 Answers1

8

1e16 is, in Scheme terms, an inexact number (or in more practical terms, a floating-point number). For (double-precision) floating point numbers above 253 (9007199254740992), the ulp is greater than 1, which means changes by 1 are too small to be represented.

10000000000000000, on the other hand, is an integer. If you do (- 10000000000000000 1), you will indeed get back 999999999999999. In Scheme, you can also write #e1e16 to represent the same quantity; the #e prefix makes the number exact (in Scheme terms; in practical terms, it means either an integer or rational).

† Try evaluating (+ 9007199254740992.0 1) and (- 9007199254740992.0 1), then try evaluating (+ 9007199254740992 1) and (- 9007199254740992 1) for contrast with integers.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
C. K. Young
  • 219,335
  • 46
  • 382
  • 435