In
Scheme, the numerical tower is a set of
data types
In computer science and computer programming, a data type (or simply type) is a collection or grouping of data values, usually specified by a set of possible values, a set of allowed operations on these values, and/or a representation of these ...
that represent
number
A number is a mathematical object used to count, measure, and label. The most basic examples are the natural numbers 1, 2, 3, 4, and so forth. Numbers can be represented in language with number words. More universally, individual numbers can ...
s and a logic for their hierarchical organisation.

Each type in the tower conceptually "sits on" a more fundamental type, so an
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
is a
rational number
In mathematics, a rational number is a number that can be expressed as the quotient or fraction of two integers, a numerator and a non-zero denominator . For example, is a rational number, as is every integer (for example,
The set of all ...
and a
number
A number is a mathematical object used to count, measure, and label. The most basic examples are the natural numbers 1, 2, 3, 4, and so forth. Numbers can be represented in language with number words. More universally, individual numbers can ...
, but the converse is not necessarily true, i.e. not every number is an integer. This asymmetry implies that a language can safely allow
implicit coercions of numerical types—without creating semantic problems—in only one direction: coercing an integer to a rational loses no information and will never influence the value returned by a function, but to coerce most reals to an integer would alter any relevant computation (e.g., the real 1/3 does not equal any integer) and is thus impermissible.
In Scheme
Principally, the numerical tower is designed to codify the
set theoretic properties of numbers in an easy-to-implement language facility: every integer is a rational with an implicit denominator of 1, and all reals are complex with an implicit
imaginary part
In mathematics, a complex number is an element of a number system that extends the real numbers with a specific element denoted , called the imaginary unit and satisfying the equation i^= -1; every complex number can be expressed in the form ...
of 0. Practically, the implementation may save time and space by ignoring these properties unless they become arithmetically relevant, and also may correspondingly improve the efficiency of its representation when reducing numerical values to their
canonical representation by eliminating negligible components of a number.
The most generic type,
number
, is somewhat confusingly named: it exists to capture all mathematical values whose type is more general than
complex
Complex commonly refers to:
* Complexity, the behaviour of a system whose components interact in multiple ways so possible interactions are difficult to describe
** Complex system, a system composed of many components which may interact with each ...
, but which are still usable with standard mathematical operations, as defined by Scheme. Thus it captures, for example, positive and negative infinity (
+inf.0
and
-inf.0
, the
significand
The significand (also coefficient, sometimes argument, or more ambiguously mantissa, fraction, or characteristic) is the first (left) part of a number in scientific notation or related concepts in floating-point representation, consisting of its s ...
here meaning approximation
up to Two Mathematical object, mathematical objects and are called "equal up to an equivalence relation "
* if and are related by , that is,
* if holds, that is,
* if the equivalence classes of and with respect to are equal.
This figure of speech ...
cardinality), since those are mathematical objects to which at least some numerical operations may validly apply (e.g. one can add to or multiply by infinity, yielding infinity; or compare cardinality against infinity, with infinity always being greater than any finite value). On a more technical level,
number
in Lisp simply provides a place in the type hierarchy for the kinds of non-strictly-numerical values defined by
IEEE 754
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard #Design rationale, add ...
.
The Scheme programming language defines all its arithmetic within this model. Some implementations may extend or adapt the tower.
Kawa, a Scheme implementation for the
JVM
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally descri ...
, extends the tower to include both
quaternion
In mathematics, the quaternion number system extends the complex numbers. Quaternions were first described by the Irish mathematician William Rowan Hamilton in 1843 and applied to mechanics in three-dimensional space. The algebra of quater ...
s and quantities, with quantities being a way of
subtyping
In programming language theory, subtyping (also called subtype polymorphism or inclusion polymorphism) is a form of type polymorphism. A ''subtype'' is a datatype that is related to another datatype (the ''supertype'') by some notion of substi ...
numerical values with units; e.g. a number of
gram
The gram (originally gramme; SI unit symbol g) is a Physical unit, unit of mass in the International System of Units (SI) equal to one thousandth of a kilogram.
Originally defined in 1795 as "the absolute Mass versus weight, weight of a volume ...
s cannot meaningfully be added to a number of
metre
The metre (or meter in US spelling; symbol: m) is the base unit of length in the International System of Units (SI). Since 2019, the metre has been defined as the length of the path travelled by light in vacuum during a time interval of of ...
s because via quantities numbers
inherit logic derived from
dimensional analysis to govern their meaning in relation to and thus valid arithmetical interactions with each other.
Another common variation is to support both
exact and inexact versions of the tower or parts of it; R
7RS Scheme recommends but does not strictly require this of implementations. In this case, similar semantics are used to determine the permissibility of implicit coercion: inexactness is a contagious property of numbers, and any numerical operation involving both exact and inexact values must yield inexact return values of at least the
same precision as the most precise inexact number appearing in the expression, unless the precision is practically infinite (e.g. containing a detectable
repetend), or unless it can be proven that the precision of the result of the operation is independent of the inexactness of any of its operands (for example, a series of multiplications where at least one multiplicand is 0).
In other languages
Most programming languages and language implementations do not support a Scheme-like numerical tower, though some languages provide limited or inconsistent support if implementation simplicity permits.
Python, for example, provides a similar structure via PEP3141, citing Scheme's example, though in practice rational numbers (
fractions
) must be imported from their own module, and both rational and complex numbers use slightly variant syntax from normal number literals, since Python's syntax is less explicit than Lisp's.
Thus in the following Scheme examples we see:
1 -2 +3
⇒ 1
⇒ -2
⇒ 3
1/3
⇒ 1/3
72/6+8/3i
⇒ 12+8/3i ; coercion: canonical form
(+ 3+2i 2-2i)
⇒ 5 ; coercion: canonical form
(- 3-62/32i 1+inf.0i)
⇒ 2-inf.0i ; coercion: infinite cardinality
(> 3+0/2i 3)
⇒ #f ; coercion: 3 ≯ 3
While in the following Python examples we see:
1; -2; +3
⇒ 1
⇒ -2
⇒ 3
1/3
⇒ 0.3333333333333333
inf = float('inf') # infinity not first-class
from fractions import Fraction
x = Fraction(1, 3)
y = Fraction(2, 3)
x + y
⇒ Fraction(1, 1) # no coercion
(3+2j)
⇒ (3+2j)
complex(x, inf)
⇒ (0.3333333333333333+infj) # coercion: equality violated
a = 1/3
b = Fraction(1, 3)
caz = complex(a, 0)
cbz = complex(b, 0)
a b
⇒ False
caz cbz
⇒ True # proof of equality violation
complex(x + y, -inf)
⇒ (1-infj) # coercion: equality preserved
(3+0j) > 3
⇒ Traceback (most recent call last):
⇒ File "", line 1, in # no coercion: type error
⇒ TypeError: '>' not supported between instances of 'complex' and 'int'
In the Python examples, we can see that numerical issues freely arise with an inconsistent application of the semantics of its type coercion. While
1 / 3
in Python is treated as a call to divide 1 by 3, yielding a float, the inclusion of rationals inside a complex number, though clearly permissible, implicitly coerces them from rationals into floats or ints, even in cases where this is incorrect.
Smalltalk
Smalltalk is a purely object oriented programming language (OOP) that was originally created in the 1970s for educational use, specifically for constructionist learning, but later found use in business. It was created at Xerox PARC by Learni ...
is another programming language that follows this model, but it has ArithmeticValue and Magnitude as superclasses of Number.
The Numerical Tower of Scheme was inspired by the hierarchy of numeric types in
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
.
Number
/ \
Real Complex
/ \
Rational Float
/ \
Integer Ratio
References
{{DEFAULTSORT:Numerical Tower
Data types