Numbers


One of a series of tutorials about Scheme in general
and the Wraith Scheme interpreter in particular.

Copyright © 2011 Jay Reynolds Freeman, all rights reserved.
Personal Web Site: http://JayReynoldsFreeman.com
EMail: Jay_Reynolds_Freeman@mac.com.


In Scheme, numbers are typed in forms that generally resemble the forms that most people use every day. Therefore, there is one simple truth that you should remember: In Scheme, pretty much everything that looks like a number actually is a number. There are a few details beyond that, and there are some procedures involving numbers that you might want to know about, but mostly it simple: As far as Scheme is concerned, all of the following are numbers, and Scheme will understand their values to be just what you would expect if you lived in a world that had never heard of computers and programming languages.

Now let's see about some details.

You have already seen many of the predicates and other procedures that work with numbers in earlier tutorials. I will mention some of the previous ones again here, and will add a few new ones.

To begin with, there are predicates "exact?" and "inexact?", to access the information stored with each number about whether it is exact or not.

Procedures "real-part", "imag-part", "magnitude", and "angle" extract information about a complex number.

There are procedures to make a complex number either from its real and imaginary parts, or from its magnitude and angle.

Procedures "numerator" and "denominator" extract the numerator and denominator from a number which remembers those two quantities separately. Procedure "rationalize" finds the simplest fraction (in the sense of smallest denominator) within a given distance of any real number. For example:

Procedure "e::derationalize" returns the 64-bit floating-point number obtained by dividing the numerator of a fraction by its denominator. Procedure "e::make-long-ratnum" allows you to construct a "fraction" by providing its numerator and denominator as arguments to a procedure rather than typing in the fraction as a literal constant:

Two procedures allow for converting between strings and numbers. Both take an optional second argument, a number that must be 2, 8, 10 or 16, to specify the base in which the conversion is to take place. If no second argument is present the base will be taken to be 10.

Note that "number->string" never prints a radix prefix.


As a reward for reading the tutorials this far, I will tell you a secret. Wraith Scheme has as an enhancement, an undocumented procedure to print out numbers with Roman numerals. The procedure is called "e::umber-nay->ing-stray". It takes one argument -- a number -- and if that number is an exact integer in the range from 0 through 3999 (inclusive), it will print it out in Roman numerals. It will print all other numbers in the same manner that "number->string" would when it is printing in base ten.

The name "e::umber-nay->ing-stray" is in Pig Latin, which is a joke language that most Americans learned when we were in grammar school and should have been studying but weren't. If you have never heard of it, don't worry. It kind of makes sense, though; I mean, if you talk to a computer in any kind of Latin, wouldn't you expect it to use Roman numerals? This procedure is undocumented because my sense of whimsy includes leaving little tidbits of harmless unexpected features lying around in my programs for you to find, and yes, there are others.

I rather suspect that no other Scheme implementation has any procedures with names like "e::umber-nay->ing-stray". So if you go around speaking Pig Latin to other members of the Scheme community, they will probably look at you funny. If that happens, I would be very much obliged if you did not mention my name.


-- Jay Reynolds Freeman (Jay_Reynolds_Freeman@mac.com)


Wraith Face