6.6.2.3 Real and Rational Numbers

Mathematically, the real numbers are the set of numbers that describe all possible points along a continuous, infinite, one-dimensional line. The rational numbers are the set of all numbers that can be written as fractions p/q, where p and q are integers. All rational numbers are also real, but there are real numbers that are not rational, for example the square root of 2, and pi.

Guile can represent both exact and inexact rational numbers, but it cannot represent precise finite irrational numbers. Exact rationals are represented by storing the numerator and denominator as two exact integers. Inexact rationals are stored as floating point numbers using the C type double.

Exact rationals are written as a fraction of integers. There must be no whitespace around the slash:

1/2
-22/7

Even though the actual encoding of inexact rationals is in binary, it may be helpful to think of it as a decimal number with a limited number of significant figures and a decimal point somewhere, since this corresponds to the standard notation for non-whole numbers. For example:

0.34
-0.00000142857931198
-5648394822220000000000.0
4.0

The limited precision of Guile’s encoding means that any finite “real” number in Guile can be written in a rational form, by multiplying and then dividing by sufficient powers of 10 (or in fact, 2). For example, ‘-0.00000142857931198’ is the same as −142857931198 divided by 100000000000000000. In Guile’s current incarnation, therefore, the rational? and real? predicates are equivalent for finite numbers.

Dividing by an exact zero leads to a error message, as one might expect. However, dividing by an inexact zero does not produce an error. Instead, the result of the division is either plus or minus infinity, depending on the sign of the divided number and the sign of the zero divisor (some platforms support signed zeroes ‘-0.0’ and ‘+0.0’; ‘0.0’ is the same as ‘+0.0’).

Dividing zero by an inexact zero yields a NaN (‘not a number’) value, although they are actually considered numbers by Scheme. Attempts to compare a NaN value with any number (including itself) using =, <, >, <= or >= always returns #f. Although a NaN value is not = to itself, it is both eqv? and equal? to itself and other NaN values. However, the preferred way to test for them is by using nan?.

The real NaN values and infinities are written ‘+nan.0’, ‘+inf.0’ and ‘-inf.0’. This syntax is also recognized by read as an extension to the usual Scheme syntax. These special values are considered by Scheme to be inexact real numbers but not rational. Note that non-real complex numbers may also contain infinities or NaN values in their real or imaginary parts. To test a real number to see if it is infinite, a NaN value, or neither, use inf?, nan?, or finite?, respectively. Every real number in Scheme belongs to precisely one of those three classes.

On platforms that follow IEEE 754 for their floating point arithmetic, the ‘+inf.0’, ‘-inf.0’, and ‘+nan.0’ values are implemented using the corresponding IEEE 754 values. They behave in arithmetic operations like IEEE 754 describes it, i.e., (= +nan.0 +nan.0)#f.

Scheme Procedure: real? obj
C Function: scm_real_p (obj)

Return #t if obj is a real number, else #f. Note that the sets of integer and rational values form subsets of the set of real numbers, so the predicate will also be fulfilled if obj is an integer number or a rational number.

Scheme Procedure: rational? x
C Function: scm_rational_p (x)

Return #t if x is a rational number, #f otherwise. Note that the set of integer values forms a subset of the set of rational numbers, i.e. the predicate will also be fulfilled if x is an integer number.

Scheme Procedure: rationalize x eps
C Function: scm_rationalize (x, eps)

Returns the simplest rational number differing from x by no more than eps.

As required by R5RS, rationalize only returns an exact result when both its arguments are exact. Thus, you might need to use inexact->exact on the arguments.

(rationalize (inexact->exact 1.2) 1/100)
⇒ 6/5
Scheme Procedure: inf? x
C Function: scm_inf_p (x)

Return #t if the real number x is ‘+inf.0’ or ‘-inf.0’. Otherwise return #f.

Scheme Procedure: nan? x
C Function: scm_nan_p (x)

Return #t if the real number x is ‘+nan.0’, or #f otherwise.

Scheme Procedure: finite? x
C Function: scm_finite_p (x)

Return #t if the real number x is neither infinite nor a NaN, #f otherwise.

Scheme Procedure: nan
C Function: scm_nan ()

Return ‘+nan.0’, a NaN value.

Scheme Procedure: inf
C Function: scm_inf ()

Return ‘+inf.0’, positive infinity.

Scheme Procedure: numerator x
C Function: scm_numerator (x)

Return the numerator of the rational number x.

Scheme Procedure: denominator x
C Function: scm_denominator (x)

Return the denominator of the rational number x.

C Function: int scm_is_real (SCM val)
C Function: int scm_is_rational (SCM val)

Equivalent to scm_is_true (scm_real_p (val)) and scm_is_true (scm_rational_p (val)), respectively.

C Function: double scm_to_double (SCM val)

Returns the number closest to val that is representable as a double. Returns infinity for a val that is too large in magnitude. The argument val must be a real number.

C Function: SCM scm_from_double (double val)

Return the SCM value that represents val. The returned value is inexact according to the predicate inexact?, but it will be exactly equal to val.