Why does Racket report that π is rational?
Asked Answered
A

1

6

As any secondary math student can attest, pi is irrational.

And yet:

Welcome to Racket v5.3.6.
> pi
3.141592653589793
> (rational? pi)
#t

Is this because the representation of pi, in the underlying machine's floating point format, is of limited precision and therefore can always be expressed as some p/q where q is 10^n, and n is the representational precision ?

If so, how could any number thrown about by Racket (or other similarly-behaving scheme) ever be considered anything but rational? And hence, why bother with the rational? function?

UPDATE: Even (rational? (sqrt 3)) reports #t

Almund answered 28/10, 2017 at 5:30 Comment(9)
Do you know some number for which Racket reports that rational? gives #f ? BTW is pi predefined? How is it documented (as the real pi, or as some approximation of it)?Lout
I do not. I'm new to scheme, was surprised that (rational? 4.1) reported true, and then thought I'd go with pi as my nuclear option. My head promptly asploded.Almund
I even tried a (leibniz err) function that I found online that computes approximations of pi to within the given error. Its result also was declared to be rational.Almund
That looks to me like a rational number because, as you said, it is only pi to a few decimal places. My guess is that some results of functions, for instance some square roots like sqrt(3), would be irrational when calculated, and the function would recognize that.Discreditable
BTW, I would ask that on some forum or mailing list dedicated to Racket. FWIW, guile (and bigloo) don't even know about pi, and is also a Scheme implementation.Lout
@Discreditable (rational? (sqrt 3)) => #tAlmund
I guess not then.Discreditable
(rational? (sqrt 3)) => #t because all real numbers other than the infinities and NaNs are rational.Runt
Many numbers supported by many Schemes are not rational: in particular no complex number with a non-zero imaginary part is rational. Thus, for instance, (rational? (sqrt -1)) is false.Borrero
R
4

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.

Runt answered 28/10, 2017 at 5:41 Comment(4)
After thinking about it more, I suppose a robust rational? would risk boiling the oceans...Almund
Agreed. I'm curious about whether that is even computable. One would need to know what functions like sqrt were intended to mean. And I don't think getting into people heads like that can rationally be expected of a language.Runt
Racket inherited rational? from Scheme, which allowed implementations a lot of flexibility in the numeric system. I think other Schemes might have had more reals that weren't rational, numbers that weren't complex (quaternions?), etc.Puklich
Very true. We were commenting though on the difficulty/impossibility? of detecting calls such as sqrt(3) and attempting to make THAT value return #f when passed to rational? Attempting to determine which sqrts, which sines, which cosines etc really are irrational wouldn't be possible especially when there are user-defined transcendentals that could be created. At any rate, yes, good point that there's nothing really "wrong" or even that bizzare with Racket's implementation.Runt

© 2022 - 2024 — McMap. All rights reserved.