When the Racket guide says that integers are “exact”, does that mean so as long as my operations are closed around integers, then I don’t have to worry about that floating point correction stuff?
If you stick to exact operations on exact inputs, then yes, there will be no loss of precision, unlike floating-point arithmetic.
whoa that’s pretty cool
Basic numeric operations like addition, subtraction, multiplication, division, and exponentiation will preserve exactness, but some operations will not (like sqrt
, since Racket does not have any exact representation of irrational numbers).
Likewise, pi
and e
are inexact numbers, and any computation using them will therefore become inexact.
i see
(Floating-point numbers are also much faster than exact rationals, since your computer has hardware specifically for the purpose of making floating-point operations fast.)
(And floating-point numbers also have the advantage of taking up a constant amount of memory, whereas exact numbers are arbitrary-precision, so arbitrarily large numbers can take up arbitrarily-large amounts of memory.)
Yeah but I know there’s some machinery involved in working with floating points
Like you have to compare to an epsilon or something
Yes. Racket’s exact numbers are like BigInteger
and BigDecimal
in Java, if you are at all familiar with that.