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 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?
.
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.
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 satisfiesrational?
in Guile.
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 useinexact->exact
on the arguments.(rationalize (inexact->exact 1.2) 1/100) => 6/5
Return
#t
if x is either `+inf.0' or `-inf.0',#f
otherwise.
Return the numerator of the rational number x.
Return the denominator of the rational number x.
Equivalent to
scm_is_true (scm_real_p (val))
andscm_is_true (scm_rational_p (val))
, respectively.