Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Numerical tower
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Set of data types that represent numbers in a given programming language}} In [[Scheme (programming language)|Scheme]], the '''numerical tower''' is a set of [[data types]] that represent [[number]]s and a logic for their hierarchical organisation. [[File:NumericalTower.svg|thumb|A representation of the numerical tower with five types of numbers.]] Each type in the tower conceptually "sits on" a more fundamental type, so an [[integer]] is a [[rational number]] and a [[number]], but the converse is not necessarily true, i.e. not every number is an integer. This asymmetry implies that a language can safely allow [[Type conversion|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 theory|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 number|imaginary part]] 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 form|canonical representation]] by eliminating negligible components of a number. The most generic type, <code>number</code>, is somewhat confusingly named: it exists to capture all mathematical values whose type is more general than [[Complex number|complex]], but which are still usable with standard mathematical operations, as defined by Scheme. Thus it captures, for example, positive and negative infinity (<code>+inf.0</code> and <code>-inf.0</code>, the [[significand]] here meaning approximation [[up to]] 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).<ref>{{Cite web|url=https://small.r7rs.org/attachment/r7rs.pdf#subsection.6.2.4|title=Revised<sup>7</sup> Report on the Algorithmic Language Scheme: 6.2.4: Implementation extensions}}</ref> On a more technical level, <code>number</code> in Lisp simply provides a place in the type hierarchy for the kinds of non-strictly-numerical values defined by [[IEEE 754]]. The Scheme programming language defines all its arithmetic within this model.<ref>{{Cite web|url=https://www.schemers.org/Documents/Standards/R5RS/r5rs.pdf#numericaltypes|title=Revised<sup>5</sup> Report on the Algorithmic Language Scheme: 6.2.1: Numerical types}}</ref><ref>{{Cite web|url=https://small.r7rs.org/attachment/r7rs.pdf#subsection.6.2.1|title=Revised<sup>7</sup> Report on the Algorithmic Language Scheme: 6.2.1: Numerical types}}</ref> Some implementations may extend or adapt the tower. [[Kawa (Scheme implementation)|Kawa]], a Scheme implementation for the [[JVM]], extends the tower to include both [[quaternion]]s<ref>{{Cite web|url=https://www.gnu.org/software/kawa/Quaternions.html|title=Kawa Reference Documentation: 12.4. Quaternions}}</ref> and quantities,<ref>{{Cite web|url=https://www.gnu.org/software/kawa/Quantities.html|title=Kawa Reference Documentation: 12.5 Quantities and Units}}</ref> with quantities being a way of [[subtyping]] numerical values with units; e.g. a number of [[gram]]s cannot meaningfully be added to a number of [[metre]]s because via quantities numbers [[Inheritance (object-oriented programming)|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 [[Floating-point arithmetic|exact and inexact]] versions of the tower or parts of it; R<sup>7</sup>RS 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,<ref>{{Cite web|url=https://small.r7rs.org/attachment/r7rs.pdf#subsection.6.2.2|title=Revised<sup>7</sup> Report on the Algorithmic Language Scheme: 6.2.2: Exactness}}</ref> and any numerical operation involving both exact and inexact values must yield inexact return values of at least the [[Significant figures|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 (programming language)|Python]], for example, provides a similar structure via PEP3141,<ref>{{Cite web|url=https://peps.python.org/pep-3141/|title=PEP 3141 – A Type Hierarchy for Numbers}}</ref> citing Scheme's example, though in practice rational numbers (<code>fractions</code>) 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: <syntaxhighlight lang="Scheme"> 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 </syntaxhighlight> While in the following Python examples we see: <syntaxhighlight lang="Python"> 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 "<stdin>", line 1, in <module> # no coercion: type error ⇒ TypeError: '>' not supported between instances of 'complex' and 'int' </syntaxhighlight> In the Python examples, we can see that numerical issues freely arise with an inconsistent application of the semantics of its type coercion. While <code>1 / 3</code> 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]] 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]].<ref>{{Cite web|url=https://standards.scheme.org/official/r2rs.pdf#numbers|title=Revised<sup>2</sup> Report on Scheme: II.6.: Numbers}}</ref><ref>{{Cite web|url=https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node16.html#SECTION00610000000000000000|title=Common Lisp the Language, 2nd Edition: 2.1: Numbers}}</ref> <pre> Number / \ Real Complex / \ Rational Float / \ Integer Ratio </pre> ==References== <references/> {{DEFAULTSORT:Numerical Tower}} [[Category:Data types]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:Short description
(
edit
)