The number returned by pi
is rational because the documentation says so. Specifically it says:
All numbers are complex numbers. Some of them are real numbers, and all of the real numbers that can be represented are also rational numbers, except for +inf.0 (positive infinity), +inf.f (single-precision variant), -inf.0 (negative infinity), -inf.f (single-precision variant), +nan.0 (not-a-number), and +nan.f (single-precision variant). Among the rational numbers, some are integers, because round applied to the number produces the same number.
So your hunch is right. All representable real numbers are indeed rational (except for the infinities and NaNs) because, yes, numbers are stored in fixed-size registers so the machine isn't going to store an irrational number.
As to why the Racket designers bothered with a rational?
function, that is a good question. Many languages like Julia and Clojure have a real, actual, honest-to-goodness rational datatype. Racket doesn't, so, as you suspect, it does seem silly to define a near-complete subset of the reals as rationals.
But you know, it just may be convenient to have a way to talk about a non-NaN, non-Infinity value. I would have called it finite
, but Racket calls it rational
.
rational?
gives#f
? BTW ispi
predefined? How is it documented (as the real pi, or as some approximation of it)? – Loutpi
to a few decimal places. My guess is that some results of functions, for instance some square roots likesqrt(3)
, would be irrational when calculated, and the function would recognize that. – Discreditableguile
(andbigloo
) don't even know aboutpi
, and is also a Scheme implementation. – Lout(rational? (sqrt 3)) => #t
because all real numbers other than the infinities and NaNs are rational. – Runt(rational? (sqrt -1))
is false. – Borrero