Next: Complex Numbers, Previous: Integers, Up: 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 represents both real and rational numbers approximately using a floating point encoding with limited precision. Even though the actual encoding 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.
Another aspect of this equivalence is that Guile currently does not preserve the exactness that is possible with rational arithmetic. If such exactness is needed, it is of course possible to implement exact rational arithmetic at the Scheme level using Guile's arbitrary size integers.
A planned future revision of Guile's numerical tower will make it possible to implement exact representations and arithmetic for both rational numbers and real irrational numbers such as square roots, and in such a way that the new kinds of number integrate seamlessly with those that are already implemented.
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’,
respectibly. 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?.
Return
#tif 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.
Return
#tif x is a rational number,#fotherwise. 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. Real numbers will also satisfy this predicate, because of their limited precision.