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

Cognitive Hazard
  • 1,072
  • 10
  • 25
  • 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)? – Basile Starynkevitch Oct 28 '17 at 05:36
  • 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. – Cognitive Hazard Oct 28 '17 at 05:38
  • 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. – Cognitive Hazard Oct 28 '17 at 05:39
  • 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. – the_storyteller Oct 28 '17 at 05:39
  • 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. – Basile Starynkevitch Oct 28 '17 at 05:39
  • @the_storyteller (rational? (sqrt 3)) => #t – Cognitive Hazard Oct 28 '17 at 05:42
  • I guess not then. – the_storyteller Oct 28 '17 at 05:43
  • `(rational? (sqrt 3)) => #t` because all real numbers other than the infinities and NaNs are rational. – Ray Toal Oct 28 '17 at 05:46
  • 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. – ignis volens May 14 '23 at 10:36

1 Answers1

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.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • After thinking about it more, I suppose a robust `rational?` would risk boiling the oceans... – Cognitive Hazard Oct 28 '17 at 05:50
  • 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. – Ray Toal Oct 28 '17 at 05:52
  • 2
    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. – Ryan Culpepper Oct 28 '17 at 17:32
  • 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. – Ray Toal Oct 28 '17 at 23:24