Re: The Rational Type
Taylor R Campbell <[email protected]> Wed, 1 Jan 2020 19:46:25 +0000
| Newsgroups | gmane.lisp.scheme.scheme48 |
|---|---|
| Message-ID | <[email protected]> |
(Consider setting your mailer to send plain text, not HTML-only?) > Date: Wed, 1 Jan 2020 20:45:32 +0200 > From: Amit Yaron <[email protected]> > > I've found that Scheme48 prints the results of dividing one integer > by another in a fraction notation. So, I tried the function > 'rational?', which returns true when the operand is a number, even > if it is a known irrational one. Try (rational? 1+2i). > Following are some results: > > (rational? (sqrt 2)) > #t > > (rational? (exp 1)) > #t > > 6> (define pi (* 2 (asin 1))) > ; no values returned > 6> pi > 3.141592653589793 > 6> (rational? pi) > #t [Side note: From the `6>' prompt, you seem to be working six interaction levels deep, probably after six errors -- Scheme48 has kept the state around in case you want to debug them with ,preview (stack trace) or similar. If you're not interested in debugging, you can return back to the top level by running the ,reset command, or ,pop to go back a single level.] > Is it supposed to work this way? What you get by evaluating (sqrt 2), (exp 1), (* 2 (asin 1)), &c., is a rational approximation to the number you requested -- specifically, a floating-point approximation, of the form 2^e (1 + f/2^52) where e and f are integers, respectively limited to [-1022,1023] and [0,2^52). (There are also a few other floating-point numbers, called subnormals, but they too are rational numbers; and a few other floating-point values, namely infinities and NaNs, but whatever they are, they are not irrational numbers either.) In principle, a Scheme system could represent some real irrational numbers exactly. For example: 1. We could represent any algebraic number by its minimal polynomial over Q. 2. We could represent a handful of transcendental numbers like pi and e by a Q-linear combination of a basis (1, pi, e) for an extension Q(pi,e) of Q -- just like we already do for the `rectangular' representation of complex numbers as linear combinations of the basis (1, i) for the extension Q(sqrt(-1)) of Q. 3. We could represent any computable real number by a procedure that computes the successive continuants or convergents of its continued fraction representation. But Scheme48 does none of these, for various reasons -- (1) is limited to algebraic numbers and so isn't too useful for exp or asin, (2) is limited to a handful of transcendental numbers and it's not clear a priori which ones should go into the basis, and while in principle there's no limit to what numbers (3) can cover except the very modest requirement that they be computable, performing calculations with (3) is not entirely trivial. So the only real numbers you get in Scheme48 are rational numbers. Which is good enough for numerical algorithms because for any real number you want, there's a rational number as close as you need to that real number, and even a floating-point rational approximation is generally good enough. (There are, of course, unimaginably more real numbers than you could ever encounter in the real world! The `real' numbers are in a certain sense a little less real and a little more imaginary than the `imaginary' numbers -- there's a simple computable algebraic construction of the `imaginary' numbers, yet the incomprehensibly vast majority of `real' numbers cannot even a have computer program, say, print out their digits...)