An exact number is either
an arbitrarily large or small integer, such as 5, 99999999999999999, or -17;
a rational that is exactly the ratio of two arbitrarily small or large integers, such as 1/2, 99999999999999999/2, or -3/4; or
a complex number with exact real and imaginary parts (where the imaginary part is not zero), such as 1+2i or 1/2+3/4i.
An inexact number is either
an IEEE floating-point representation of a number, such as 2.0 or 3.14e+87, where the IEEE infinities and not-a-number are written +inf.0, -inf.0, and +nan.0 (or -nan.0); or
a complex number with real and imaginary parts that are IEEE floating-point representations, such as 2.0+3.0i or -inf.0+nan.0i; as a special case, an inexact complex number can have an exact zero real part with an inexact imaginary part.
Inexact numbers print with a decimal point or exponent specifier, and exact numbers print as integers and fractions. The same conventions apply for reading number constants, but #e or #i can prefix a number to force its parsing as an exact or inexact number. The prefixes #b, #o, and #x specify binary, octal, and hexadecimal interpretation of digits.
Computations that involve an inexact number produce inexact results, so that inexactness acts as a kind of taint on numbers. Beware, however, that Scheme offers no “inexact booleans”, so computations that branch on the comparison of inexact numbers can nevertheless produce exact results. The procedures exact->inexact and inexact->exact convert between the two types of numbers.
> (/ 1 2)
> (/ 1 2.0)
> (inexact->exact 0.1)
Inexact results are also produced by procedures such as sqrt, log, and sin when an exact result would require representing real numbers that are not rational. Scheme can represent only rational numbers and complex numbers with rational parts.
> (sin 0) ; rational...
> (sin 1/2) ; not rational...
In terms of performance, computations with small integers are typically the fastest, where “small” means that the number fits into one bit less than the machine’s word-sized representation for signed numbers. Computation with very large exact integers or with non-integer exact numbers can be much more expensive than computation with inexact numbers.
(define (sigma f a b)
cpu time: 126 real time: 125 gc time: 0
cpu time: 484 real time: 483 gc time: 483
The number categories integer, rational, real (always rational), and complex are defined in the usual way, and are recognized by the procedures integer?, rational?, real?, and complex?, in addition to the generic number?. A few mathematical procedures accept only real numbers, but most implement standard extensions to complex numbers.
> (integer? 5)
> (complex? 5)
> (integer? 5.0)
> (integer? 1+2i)
> (complex? 1+2i)
> (complex? 1.0+2.0i)
> (abs -5)
> (abs -5+2i)
abs: expects argument of type <real number>; given -5+2i
> (sin -5+2i)
The = procedure compares numbers for numerical equality. If it is given both inexact and exact numbers to compare, it essentially converts the inexact numbers to exact before comparing. The eqv? (and therefore equal?) procedure, in contrast, compares numbers considering both exactness and numerical equality.
> (= 1 1.0)
> (eqv? 1 1.0)
Beware of comparisons involving inexact numbers, which by their nature can have surprising behavior. Even apparently simple inexact numbers may not mean what you think they mean; for example, while a base-2 IEEE floating-point number can represent 1/2 exactly, it can only approximate 1/10:
> (= 1/2 0.5)
> (= 1/10 0.1)
> (inexact->exact 0.1)