Lisp: Why does (- 1e16 1) yield 10000000000000000 not 9999999999999999?
Or rather, why does
(= 1e16 (- 1e16 1)) return true? How can I receive more accurate answer?
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.