Next: , Previous: Integers, Up: Numbers


5.5.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 can not represent 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 “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.

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.

The infinities are written `+inf.0' and `-inf.0', respectivly. This syntax is also recognized by read as an extension to the usual Scheme syntax.

Dividing zero by zero yields something that is not a number at all: `+nan.0'. This is the special `not a number' value.

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.

The infinities are inexact integers and are considered to be both even and odd. While `+nan.0' is not = to itself, it is eqv? to itself.

To test for the special values, use the functions inf? and nan?.

— 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.

Since Guile can not represent irrational numbers, every number satisfying real? also satisfies rational? in Guile.

— 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 x is either `+inf.0' or `-inf.0', #f otherwise.

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

Return #t if x is `+nan.0', #f otherwise.

— Scheme Procedure: nan
— C Function: scm_nan ()

Return NaN.

— Scheme Procedure: inf
— C Function: scm_inf ()

Return Inf.

— 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 representats val. The returned value is inexact according to the predicate inexact?, but it will be exactly equal to val.