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

      3.2.2.1 Arithmetic

      3.2.2.2 Number Comparison

      3.2.2.3 Powers and Roots

      3.2.2.4 Trignometric Functions

      3.2.2.5 Complex Numbers

      3.2.2.6 Bitwise Operations

      3.2.2.7 Random Numbers

      3.2.2.8 Number–String Conversions

      3.2.2.9 Extra Constants and Functions

    3.2.3 Flonums

      3.2.3.1 Flonum Arithmetic

      3.2.3.2 Flonum Vectors

    3.2.4 Fixnums

      3.2.4.1 Fixnum Arithmetic

      3.2.4.2 Fixnum Vectors