Next: , Up: Numbers


5.5.2.1 Scheme's Numerical “Tower”

Scheme's numerical “tower” consists of the following categories of numbers:

integers
Whole numbers, positive or negative; e.g. –5, 0, 18.
rationals
The set of numbers that can be expressed as p/q where p and q are integers; e.g. 9/16 works, but pi (an irrational number) doesn't. These include integers (n/1).
real numbers
The set of numbers that describes all possible positions along a one-dimensional line. This includes rationals as well as irrational numbers.
complex numbers
The set of numbers that describes all possible positions in a two dimensional space. This includes real as well as imaginary numbers (a+bi, where a is the real part, b is the imaginary part, and i is the square root of −1.)

It is called a tower because each category “sits on” the one that follows it, in the sense that every integer is also a rational, every rational is also real, and every real number is also a complex number (but with zero imaginary part).

In addition to the classification into integers, rationals, reals and complex numbers, Scheme also distinguishes between whether a number is represented exactly or not. For example, the result of sin(pi/4) is exactly 2^(1/2) but Guile can neither represent pi/4 nor 2^(1/2) exactly. Instead, it stores an inexact approximation, using the C type double.

Guile can represent exact rationals of any magnitude, inexact rationals that fit into a C double, and inexact complex numbers with double real and imaginary parts.

The number? predicate may be applied to any Scheme value to discover whether the value is any of the supported numerical types.

— Scheme Procedure: number? obj
— C Function: scm_number_p (obj)

Return #t if obj is any kind of number, else #f.

For example:

     (number? 3)
     => #t
     
     (number? "hello there!")
     => #f
     
     (define pi 3.141592654)
     (number? pi)
     => #t
— C Function: int scm_is_number (SCM obj)

This is equivalent to scm_is_true (scm_number_p (obj)).

The next few subsections document each of Guile's numerical data types in detail.