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
(section)
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!
== 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>
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)