Next: , Up: baselib math ops   [Index]


4.6.4.1 Numerical type predicates

Procedure: number? obj
Procedure: complex? obj
Procedure: real? obj
Procedure: rational? obj
Procedure: integer? obj

These numerical type predicates can be applied to any kind of argument. They return #t if the object is a number object of the named type, and #f otherwise. In general, if a type predicate is true of a number object then all higher type predicates are also true of that number object. Consequently, if a type predicate is false of a number object, then all lower type predicates are also false of that number object.

If z is a complex number object, then (real? z) is true if and only if (zero? (imag-part z)) and (exact? (imag-part z)) are both true.

If x is a real number object, then (rational? x) is true if and only if there exist exact integer objects k1 and k2 such that (= x (/ k1 k2)) and (= (numerator x) k1) and (= (denominator x) k2) are all true. Thus infinities and NaNs are not rational number objects.

If q is a rational number object, then (integer? q) is true if and only if (= (denominator q) 1) is true. If q is not a rational number object, then (integer? q) is #f.

Notice that the comparison function = does not care about the exactness of its arguments:

(= 1 1.1)       ⇒ #t
(= 1 #i1.1)     ⇒ #t

so, with the given definition, the only real numbers (according to real?) that are not also rational numbers (according to rational?) are infinities and NaNs. For the same reason, numbers are integers (according to integer?) without respect for exactness.

(complex? 3+4i)                        ⇒ #t
(complex? 3)                           ⇒ #t
(real? 3)                              ⇒ #t
(real? -2.5+0.0i)                      ⇒ #f
(real? -2.5+0i)                        ⇒ #t
(real? -2.5)                           ⇒ #t
(real? #e1e10)                         ⇒ #t
(rational? 6/10)                       ⇒ #t
(rational? 6/3)                        ⇒ #t
(rational? 2)                          ⇒ #t
(integer? 3+0i)                        ⇒ #t
(integer? 3.0)                         ⇒ #t
(integer? 8/4)                         ⇒ #t

(number? +nan.0)                       ⇒ #t
(complex? +nan.0)                      ⇒ #t
(real? +nan.0)                         ⇒ #t
(rational? +nan.0)                     ⇒ #f
(complex? +inf.0)                      ⇒ #t
(real? -inf.0)                         ⇒ #t
(rational? -inf.0)                     ⇒ #f
(integer? -inf.0)                      ⇒ #f

Notice that ‘3.0+0i’ is an integer number according to integer?, because the imaginary part is exact zero. ‘3.0+0.0i’ is a complex number because the imaginary part is inexact zero, and it is equal to ‘#i3.0+0i’; ‘3.0+0.0i’ is not integer according to integer?, but it is integer-valued?.

(integer? 3.0+0i)                       ⇒ #t
(integer? 3.0+0.0i)                     ⇒ #f
(integer? #i3.0+0i)                     ⇒ #f

(integer-valued? 3.0+0.0i)              ⇒ #t
(integer-valued? #i3.0+0i)              ⇒ #t

NOTE Except for number?, the behavior of these type predicates on inexact number objects is unreliable, because any inaccuracy may affect the result.

Procedure: real-valued? obj
Procedure: rational-valued? obj
Procedure: integer-valued? obj

These numerical type predicates can be applied to any kind of argument. The real-valued? procedure returns #t if the object is a number object and is equal in the sense of = to some real number object, or if the object is a NaN, or a complex number object whose real part is a NaN and whose imaginary part is zero in the sense of zero?. The rational-valued? and integer-valued? procedures return #t if the object is a number object and is equal in the sense of = to some object of the named type, and otherwise they return #f.

(real-valued? +nan.0)                  ⇒ #t
(real-valued? +nan.0+0i)               ⇒ #t
(real-valued? -inf.0)                  ⇒ #t
(real-valued? 3)                       ⇒ #t
(real-valued? -2.5+0.0i)               ⇒ #t
(real-valued? -2.5+0i)                 ⇒ #t
(real-valued? -2.5)                    ⇒ #t
(real-valued? #e1e10)                  ⇒ #t

(rational-valued? +nan.0)              ⇒ #f
(rational-valued? -inf.0)              ⇒ #f
(rational-valued? 6/10)                ⇒ #t
(rational-valued? 6/10+0.0i)           ⇒ #t
(rational-valued? 6/10+0i)             ⇒ #t
(rational-valued? 6/3)                 ⇒ #t

(integer-valued? 3+0i)                 ⇒ #t
(integer-valued? 3+0.0i)               ⇒ #t
(integer-valued? 3.0)                  ⇒ #t
(integer-valued? 3.0+0.0i)             ⇒ #t
(integer-valued? 8/4)                  ⇒ #t

NOTE These procedures test whether a given number object can be coerced to the specified type without loss of numerical accuracy. Specifically, the behavior of these predicates differs from the behavior of real?, rational?, and integer? on complex number objects whose imaginary part is inexact zero.

NOTE The behavior of these type predicates on inexact number objects is unreliable, because any inaccuracy may affect the result.

Procedure: exact? z
Procedure: inexact? z

These numerical predicates provide tests for the exactness of a quantity. For any number object, precisely one of these predicates is true.

(exact? 5)                   ⇒ #t
(inexact? +inf.0)            ⇒ #t
(inexact? +nan.0)            ⇒ #t

Next: , Up: baselib math ops   [Index]