3.2 Numbers

+Numbers in Guide: Racket 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.0 (negative infinity), and +nan.0 (not-a-number). 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 support for 32-bit inexact numbers is specifically enabled when the run-time system is built, and 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.0, and +nan.0, which have no exact form. Dividing a number by exact zero raises an exception; dividing a non-zero number other than +nan.0 by an inexact zero returns +inf.0 or -inf.0, depending on the sign of the dividend. The +nan.0 value is not = to itself, but +nan.0 is eqv? to itself. Conversely, (= 0.0 -0.0) is #t, but (eqv? 0.0 -0.0) is #f. The datum -nan.0 refers to the same constant as +nan.0.

Calculations with infinites produce results consistent with IEEE double-precision floating point where IEEE specifies the result; in cases where IEEE provides no specification (e.g., (angle +inf.0+inf.0i)), the result corresponds to the limit approaching infinity, or +nan.0 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.

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

    3.2.1 Number Types

    3.2.2 Generic Numerics Arithmetic Number Comparison Powers and Roots Trignometric Functions Complex Numbers Bitwise Operations Random Numbers Number–String Conversions Extra Constants and Functions

    3.2.3 Flonums Flonum Arithmetic Flonum Vectors

    3.2.4 Fixnums Fixnum Arithmetic Fixnum Vectors