4.2 Numbers

+Numbers in The Racket Guide introduces numbers.

All numbers are complex numbers. Some of them are real numbers, and all of the real numbers that can be represented are also rational numbers, except for +inf.0 (positive infinity), +inf.f (single-precision variant), -inf.0 (negative infinity), -inf.f (single-precision variant), +nan.0 (not-a-number), and +nan.f (single-precision variant). Among the rational numbers, some are integers, because round applied to the number produces the same number.

+See Reading Numbers for information on the syntax of number literals.

Orthogonal to those categories, each number is also either an exact number or an inexact number. Unless otherwise specified, computations that involve an inexact number produce inexact results. Certain operations on inexact numbers, however, produce an exact number, such as multiplying an inexact number with an exact 0. Some operations, which can produce an irrational number for rational arguments (e.g., sqrt), may produce inexact results even for exact arguments.

In the case of complex numbers, either the real and imaginary parts are both exact or inexact, or the number has an exact zero real part and an inexact imaginary part; a complex number with an exact zero imaginary part is a real number.

Inexact real numbers are implemented as either single- or double-precision IEEE floating-point numbers—the latter by default, and the former only when a computation starts with numerical constants specified as single-precision numbers. Inexact real numbers that are represented as double-precision floating-point numbers are flonums.

The precision and size of exact numbers is limited only by available memory (and the precision of operations that can produce irrational numbers). In particular, adding, multiplying, subtracting, and dividing exact numbers always produces an exact result.

Inexact numbers can be coerced to exact form, except for the inexact numbers +inf.0, +inf.f, -inf.0, -inf.f, +nan.0, and +nan.f, which have no exact form. Dividing a number by exact zero raises an exception; dividing a non-zero number other than +nan.0 or +nan.f by an inexact zero returns +inf.0, +inf.f, -inf.0 or -inf.f, depending on the sign and precision of the dividend. The +nan.0 value is not = to itself, but +nan.0 is eqv? to itself, and +nan.f is similarly eqv? but not = to itself. Conversely, (= 0.0 -0.0) is #t, but (eqv? 0.0 -0.0) is #f, and the same for 0.0f0 and -0.0f0 (which are single-precision variants). The datum -nan.0 refers to the same constant as +nan.0, and -nan.f is the same as +nan.f.

Calculations with infinites produce results consistent with IEEE double- or single-precision floating point where IEEE specifies the result; in cases where IEEE provides no specification, the result corresponds to the limit approaching infinity, or +nan.0 or +nan.f if no such limit exists.

A fixnum is an exact integer whose two’s complement representation fit into 31 bits on a 32-bit platform or 63 bits on a 64-bit platform; furthermore, no allocation is required when computing with fixnums. See also the racket/fixnum module, below.

Two fixnums that are = are also the same according to eq?. Otherwise, the result of eq? applied to two numbers is undefined, except that numbers produced by the default reader in read-syntax mode are interned and therefore eq? when they are eqv?.

Two numbers are eqv? when they are both inexact with the same precision or both exact, and when they are = (except for +nan.0, +nan.f, 0.0, 0.0f0, -0.0, and -0.0f0, as noted above). Two numbers are equal? when they are eqv?.

See Reading Numbers for information on reading numbers and Printing Numbers for information on printing numbers.

    4.2.1 Number Types

    4.2.2 Generic Numerics

      4.2.2.1 Arithmetic

      4.2.2.2 Number Comparison

      4.2.2.3 Powers and Roots

      4.2.2.4 Trigonometric Functions

      4.2.2.5 Complex Numbers

      4.2.2.6 Bitwise Operations

      4.2.2.7 Random Numbers

      4.2.2.8 Number–String Conversions

      4.2.2.9 Extra Constants and Functions

    4.2.3 Flonums

      4.2.3.1 Flonum Arithmetic

      4.2.3.2 Flonum Vectors

    4.2.4 Fixnums

      4.2.4.1 Fixnum Arithmetic

      4.2.4.2 Fixnum Vectors

    4.2.5 Extflonums

      4.2.5.1 Extflonum Arithmetic

      4.2.5.2 Extflonum Constants

      4.2.5.3 Extflonum Vectors

      4.2.5.4 Extflonum Byte Strings