emacs.d/clones/scheme/docs.racket-lang.org/reference/numbers.html

71 lines
29 KiB
HTML
Raw Normal View History

2022-09-30 11:00:09 +02:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>4.3&nbsp;Numbers</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="icons.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_0&quot;);">&#9658;</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="data.html" class="tocviewselflink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5&nbsp;</td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6&nbsp;</td><td><a href="mzlib_class.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7&nbsp;</td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8&nbsp;</td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9&nbsp;</td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10&nbsp;</td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11&nbsp;</td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12&nbsp;</td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13&nbsp;</td><td><a href="input-and-output.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14&nbsp;</td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15&nbsp;</td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16&nbsp;</td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17&nbsp;</td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><t
<a name="(tech._real._number)"></a><span style="font-style: italic">real numbers</span>, and all of the real numbers that can be
represented are also <a name="(tech._rational._number)"></a><span style="font-style: italic">rational numbers</span>, except for
<a name="(idx._(gentag._111._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">+inf.0</span> (positive <a name="(idx._(gentag._112._(lib._scribblings/reference/reference..scrbl)))"></a>infinity),
<a name="(idx._(gentag._113._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">+inf.f</span> (single-precision variant, when
enabled via <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read-single-flonum%29%29" class="RktValLink" data-pltdoc="x">read-single-flonum</a></span>),
<a name="(idx._(gentag._114._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal"><span class="nobreak">-i</span>nf.0</span> (negative infinity),
<a name="(idx._(gentag._115._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">-inf.f</span> (single-precision variant, when
enabled), <a name="(idx._(gentag._116._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">+nan.0</span> (<a name="(idx._(gentag._117._(lib._scribblings/reference/reference..scrbl)))"></a>not-a-number), and
<a name="(idx._(gentag._118._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">+nan.f</span> (single-precision variant, when
enabled). Among the rational numbers, some are <a name="(tech._integer)"></a><span style="font-style: italic">integers</span>,
because <span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._round%29%29" class="RktValLink" data-pltdoc="x">round</a></span> applied to the number produces the same number.</p><blockquote class="refpara"><blockquote class="refcolumn"><blockquote class="refcontent"><p><span class="imageleft"><img src="magnify.png" alt="+" width="24" height="24"/></span>See <a href="reader.html#%28part._parse-number%29" data-pltdoc="x">Reading Numbers</a> for information on the
syntax of number literals.</p></blockquote></blockquote></blockquote><p>Orthogonal to those categories, each number is also either an
<a name="(tech._exact._number)"></a><span style="font-style: italic">exact number</span> or an <a name="(tech._inexact._number)"></a><span style="font-style: italic">inexact number</span>. 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 <span class="RktVal">0</span>. Operations that mathematically produce
irrational numbers for some rational arguments (e.g., <span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._sqrt%29%29" class="RktValLink" data-pltdoc="x">sqrt</a></span>) may
produce inexact results even for exact arguments.</p><p>In the case of complex numbers, either the real and imaginary parts
are both exact or inexact with the same precision, 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.</p><p>Inexact real numbers are implemented as double-precision
<a name="(idx._(gentag._119._(lib._scribblings/reference/reference..scrbl)))"></a>IEEE floating-point numbers, also known as
<a name="(tech._flonum)"></a><span style="font-style: italic">flonums</span>, or as single-precision IEEE floating-point numbers,
also known as <a name="(tech._single._flonum)"></a><span style="font-style: italic">single-flonums</span>. Single-flonums are
supported only when <span class="RktPn">(</span><span class="RktSym"><a href="number-types.html#%28def._%28%28quote._~23~25kernel%29._single-flonum-available~3f%29%29" class="RktValLink" data-pltdoc="x">single-flonum-available?</a></span><span class="RktPn">)</span> reports
<span class="RktVal">#t</span>. Although we write <span class="RktVal">+inf.f</span>,
<span class="RktVal">-inf.f</span>, and <span class="RktVal">+nan.f</span> to mean
single-flonums, those forms read as double-precision flonums by
default, since <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read-single-flonum%29%29" class="RktValLink" data-pltdoc="x">read-single-flonum</a></span> is <span class="RktVal">#f</span> by default.
When single-flonums are supported, inexact numbers are still
represented as flonums by default, and single precision is used only
when a computation starts with single-flonums.</p><p>Inexact numbers can be coerced to exact form, except for the inexact
numbers <span class="RktVal">+inf.0</span>, <span class="RktVal">+inf.f</span>,
<span class="RktVal"><span class="nobreak">-i</span>nf.0</span>, <span class="RktVal">-inf.f</span>, <span class="RktVal">+nan.0</span>, and <span class="RktVal">+nan.f</span>, which
have no exact form. <a name="(idx._(gentag._120._(lib._scribblings/reference/reference..scrbl)))"></a>Dividing a
number by exact zero raises an exception; dividing a non-zero number
other than <span class="RktVal">+nan.0</span> or <span class="RktVal">+nan.f</span> by an inexact zero returns <span class="RktVal">+inf.0</span>,
<span class="RktVal">+inf.f</span>, <span class="RktVal"><span class="nobreak">-i</span>nf.0</span>
or <span class="RktVal">-inf.f</span>, depending on the sign and precision of the dividend. The
<span class="RktVal">+nan.0</span> value is not <span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span> to itself, but <span class="RktVal">+nan.0</span>
is <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span> to itself, and <span class="RktVal">+nan.f</span> is similarly <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span> but
not <span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span> to itself. Conversely, <span class="RktPn">(</span><span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span><span class="stt"> </span><span class="RktVal">0.0</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-0</span>.0</span><span class="RktPn">)</span> is
<span class="RktVal">#t</span>, but <span class="RktPn">(</span><span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span><span class="stt"> </span><span class="RktVal">0.0</span><span class="stt"> </span><span class="RktVal"><span class="nobreak">-0</span>.0</span><span class="RktPn">)</span> is <span class="RktVal">#f</span>, and the
same for <span class="RktVal">0.0f0</span> and <span class="RktVal">-0.0f0</span> (which are single-precision variants). The datum
<a name="(idx._(gentag._121._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">-nan.0</span> refers to the same constant as <span class="RktVal">+nan.0</span>,
and <a name="(idx._(gentag._122._(lib._scribblings/reference/reference..scrbl)))"></a><span class="RktVal">-nan.f</span> is the same as <span class="RktVal">+nan.f</span>.</p><p>Calculations with infinities produce results consistent with IEEE
double- or single-precision floating point where IEEE specifies the result; in
cases where IEEE provides no specification,
the result corresponds to the limit approaching
infinity, or <span class="RktVal">+nan.0</span> or <span class="RktVal">+nan.f</span> if no such limit exists.</p><p>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.</p><p>A <a name="(tech._fixnum)"></a><span style="font-style: italic">fixnum</span> is an exact integer whose two&rsquo;s complement
representation fits into 30 or 31 bits (depending on the Racket variant)
on a 32-bit platform or 61 or 63 bits (depending on the Racket variant) on a
64-bit platform. No allocation is required when computing
with fixnums. See also the <a href="fixnums.html" class="RktModLink" data-pltdoc="x"><span class="RktSym">racket/fixnum</span></a> module, below.</p><p>Two fixnums that are <span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span> are also the same
according to <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29" class="RktValLink" data-pltdoc="x">eq?</a></span>. Otherwise, the result of <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29" class="RktValLink" data-pltdoc="x">eq?</a></span>
applied to two numbers is undefined, except that numbers produced
by the default reader in <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read-syntax%29%29" class="RktValLink" data-pltdoc="x">read-syntax</a></span> mode are <a href="reader.html#%28tech._interned%29" class="techoutside" data-pltdoc="x"><span class="techinside">interned</span></a> and therefore <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eq~3f%29%29" class="RktValLink" data-pltdoc="x">eq?</a></span>
when they are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span>.</p><p>Two real numbers are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span> when they are both inexact with the same precision or both
exact, and when they are <span class="RktSym"><a href="generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._~3d%29%29" class="RktValLink" data-pltdoc="x">=</a></span> (except for <span class="RktVal">+nan.0</span>, <span class="RktVal">+nan.f</span>,
<span class="RktVal">0.0</span>, <span class="RktVal">+0.0f0</span>, <span class="RktVal"><span class="nobreak">-0</span>.0</span>, and <span class="RktVal">-0.0f0</span>, as noted above).
Two complex numbers are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span> when their real and imaginary parts are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span>.
Two numbers are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._equal~3f%29%29" class="RktValLink" data-pltdoc="x">equal?</a></span> when they are <span class="RktSym"><a href="Equality.html#%28def._%28%28quote._~23~25kernel%29._eqv~3f%29%29" class="RktValLink" data-pltdoc="x">eqv?</a></span>.</p><p>See <a href="reader.html#%28part._parse-number%29" data-pltdoc="x">Reading Numbers</a>
for information on <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29" class="RktValLink" data-pltdoc="x">read</a></span>ing
numbers and <a href="printing.html#%28part._print-number%29" data-pltdoc="x">Printing Numbers</a>
for information on <span class="RktSym"><a href="Writing.html#%28def._%28%28quote._~23~25kernel%29._print%29%29" class="RktValLink" data-pltdoc="x">print</a></span>ing numbers.</p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="number-types.html" class="toclink" data-pltdoc="x">4.3.1<span class="hspace">&nbsp;</span>Number Types</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html" class="toclink" data-pltdoc="x">4.3.2<span class="hspace">&nbsp;</span>Generic Numerics</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Arithmetic%29" class="toclink" data-pltdoc="x">4.3.2.1<span class="hspace">&nbsp;</span>Arithmetic</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Number_.Comparison%29" class="toclink" data-pltdoc="x">4.3.2.2<span class="hspace">&nbsp;</span>Number Comparison</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Powers_and_.Roots%29" class="toclink" data-pltdoc="x">4.3.2.3<span class="hspace">&nbsp;</span>Powers and Roots</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Trigonometric_.Functions%29" class="toclink" data-pltdoc="x">4.3.2.4<span class="hspace">&nbsp;</span>Trigonometric Functions</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Complex_.Numbers%29" class="toclink" data-pltdoc="x">4.3.2.5<span class="hspace">&nbsp;</span>Complex Numbers</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Bitwise_.Operations%29" class="toclink" data-pltdoc="x">4.3.2.6<span class="hspace">&nbsp;</span>Bitwise Operations</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Random_.Numbers%29" class="toclink" data-pltdoc="x">4.3.2.7<span class="hspace">&nbsp;</span>Random Numbers</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Other_.Randomness_.Utilities%29" class="toclink" data-pltdoc="x">4.3.2.8<span class="hspace">&nbsp;</span>Other Randomness Utilities</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Number--.String_.Conversions%29" class="toclink" data-pltdoc="x">4.3.2.9<span class="hspace">&nbsp;</span>Number&ndash;String Conversions</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="generic-numbers.html#%28part._.Extra_.Constants_and_.Functions%29" class="toclink" data-pltdoc="x">4.3.2.10<span class="hspace">&nbsp;</span>Extra Constants and Functions</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="flonums.html" class="toclink" data-pltdoc="x">4.3.3<span class="hspace">&nbsp;</span>Flonums</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="flonums.html#%28part._.Flonum_.Arithmetic%29" class="toclink" data-pltdoc="x">4.3.3.1<span class="hspace">&nbsp;</span>Flonum Arithmetic</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="flonums.html#%28part._flvectors%29" class="toclink" data-pltdoc="x">4.3.3.2<span class="hspace">&nbsp;</span>Flonum Vectors</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="fixnums.html" class="toclink" data-pltdoc="x">4.3.4<span class="hspace">&nbsp;</span>Fixnums</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="fixnums.html#%28part._.Fixnum_.Arithmetic%29" class="t