172 lines
80 KiB
HTML
172 lines
80 KiB
HTML
|
<!doctype html>
|
|||
|
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-chap-12/bc-b-number-concepts" data-has-hydrated="false">
|
|||
|
<head>
|
|||
|
<meta charset="UTF-8">
|
|||
|
<meta name="generator" content="Docusaurus v3.0.1">
|
|||
|
<title data-rh="true">12.1 Number Concepts | Common Lisp (New) Language Reference</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://lisp-docs.github.io/cl-language-reference/img/1024px-Lisp_logo.svg.png"><meta data-rh="true" name="twitter:image" content="https://lisp-docs.github.io/cl-language-reference/img/1024px-Lisp_logo.svg.png"><meta data-rh="true" property="og:url" content="https://lisp-docs.github.io/cl-language-reference/chap-12/bc-b-number-concepts"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="google-site-verification" content="Vzaw013_bfdKeUVG89Ch3W1zC9_vH9ID2dPB9Dz0vr0"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="12.1 Number Concepts | Common Lisp (New) Language Reference"><meta data-rh="true" name="description" content="12.1.1 Numeric Operations"><meta data-rh="true" property="og:description" content="12.1.1 Numeric Operations"><link data-rh="true" rel="icon" href="../img/favicon.ico"><link data-rh="true" rel="canonical" href="bc-b-number-concepts.html"><link data-rh="true" rel="alternate" href="bc-b-number-concepts.html" hreflang="en"><link data-rh="true" rel="alternate" href="bc-b-number-concepts.html" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://C1F2Q5VM6X-dsn.algolia.net" crossorigin="anonymous"><link rel="preconnect" href="https://www.google-analytics.com">
|
|||
|
<link rel="preconnect" href="https://www.googletagmanager.com">
|
|||
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-8TJCE4NSF8"></script>
|
|||
|
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-8TJCE4NSF8",{})</script>
|
|||
|
|
|||
|
|
|||
|
<link rel="search" type="application/opensearchdescription+xml" title="Common Lisp (New) Language Reference" href="../opensearch.xml"><link rel="stylesheet" href="../assets/css/styles.f13b59fe.css">
|
|||
|
<script src="../assets/js/runtime~main.02699c25.js" defer="defer"></script>
|
|||
|
<script src="../assets/js/main.4f0a7a76.js" defer="defer"></script>
|
|||
|
</head>
|
|||
|
<body class="navigation-with-keyboard">
|
|||
|
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){try{return new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}}()||function(){try{return localStorage.getItem("theme")}catch(t){}}();t(null!==e?e:"light")}(),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id="__docusaurus"><div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="bc-b-number-concepts.html#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a href="../../index.html" target="_blank" rel="noopener noreferrer" class="navbar__brand"><div class="navbar__logo"><img src="../img/logo.svg" alt="Lisp Logo" class="themedComponent_mlkZ themedComponent--light_NVdE"><img src="../img/logo.svg" alt="Lisp Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU"></div><b class="navbar__title text--truncate">Common Lisp Docs</b></a><a href="../../docs/tutorial/index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Tutorial</a><a href="../index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Technical Reference</a><a href="../../docs/whylisp.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Why Lisp?</a><a href="../../docs/howto.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Guides</a></div><div class="navbar__items navbar__items--right"><a href="../../docs/contribute.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Contribute!</a><a href="../../docs/help.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Getting Help</a><a href="../../docs/about.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">About</a><a href="../../blog.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Blog</a><a href="https://github.com/lisp-docs" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a><div class="toggle_vylO colorModeToggle_DEke"><button class="clean-btn toggleButton_gllP toggleButtonDisabled_aARS" type="button" disabled="" title="Switch between dark and light mode (currently light mode)" aria-label="Switch between dark and light mode (currently light mode)" aria-live="polite"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_pyhR"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.9
|
|||
|
<!-- -->
|
|||
|
<!-- -->
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="1211-numeric-operations">12.1.1 Numeric Operations<a href="bc-b-number-concepts.html#1211-numeric-operations" class="hash-link" aria-label="Direct link to 12.1.1 Numeric Operations" title="Direct link to 12.1.1 Numeric Operations"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p>Common Lisp provides a large variety of operations related to <span><i>numbers</i></span>. This section provides an overview of those operations by grouping them into categories that emphasize some of the relationships among them.</p>
|
|||
|
<p>Figure 12–1 shows <span><i>operators</i></span> relating to arithmetic operations.</p>
|
|||
|
<p>|</p><p><strong>* 1+ gcd</strong> </p><p><strong>+ 1- incf</strong> </p><p><strong>- conjugate lcm</strong> </p><p><strong>/ decf</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–1. Operators relating to Arithmetic.</strong></p>
|
|||
|
<p>Figure 12–2 shows <span><i>defined names</i></span> relating to exponential, logarithmic, and trigonometric operations.</p>
|
|||
|
<p>|</p><p><strong>abs cos signum</strong> </p><p><strong>acos cosh sin</strong> </p><p><strong>acosh exp sinh</strong> </p><p><strong>asin expt sqrt</strong> </p><p><strong>asinh isqrt tan</strong> </p><p><strong>atan log tanh</strong> </p><p><strong>atanh phase</strong> </p><p><strong>cis pi</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–2. Defined names relating to Exponentials, Logarithms, and Trigonometry.</strong> Figure 12–3 shows <span><i>operators</i></span> relating to numeric comparison and predication.</p>
|
|||
|
<p>|</p><p><span><b>/=</b></span> ><strong>= oddp</strong> </p><p>< <strong>evenp plusp</strong> </p><p><<strong>= max zerop</strong> </p><p><strong>= min</strong> </p><p>> <span><b>minusp</b></span></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–3. Operators for numeric comparison and predication.</strong></p>
|
|||
|
<p>Figure 12–4 shows <span><i>defined names</i></span> relating to numeric type manipulation and coercion.</p>
|
|||
|
<p>|</p><p><strong>ceiling float-radix rational</strong> </p><p><strong>complex float-sign rationalize decode-float floor realpart</strong> </p><p><strong>denominator fround rem</strong> </p><p><strong>fceiling ftruncate round</strong> </p><p><strong>ffloor imagpart scale-float float integer-decode-float truncate</strong> </p><p><strong>float-digits mod</strong> </p><p><strong>float-precision numerator</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–4. Defined names relating to numeric type manipulation and coercion.</strong></p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="12111-associativity-and-commutativity-in-numeric-operations">12.1.1.1 Associativity and Commutativity in Numeric Operations<a href="bc-b-number-concepts.html#12111-associativity-and-commutativity-in-numeric-operations" class="hash-link" aria-label="Direct link to 12.1.1.1 Associativity and Commutativity in Numeric Operations" title="Direct link to 12.1.1.1 Associativity and Commutativity in Numeric Operations"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<p>For functions that are mathematically associative (and possibly commutative), a <span><i>conforming implementation</i></span> may process the <span><i>arguments</i></span> in any manner consistent with associative (and possibly commutative) rearrangement. This does not affect the order in which the <span><i>argument</i></span></p>
|
|||
|
<p><span><i>forms</i></span> are <em>evaluated</em>; for a discussion of evaluation order, see Section 3.1.2.1.2.3 (Function Forms). What is unspecified is only the order in which the <em>parameter values</em> are processed. This implies that <span><i>implementations</i></span> may differ in which automatic <em>coercions</em> are applied; see Section 12.1.1.2 (Contagion in Numeric Operations).</p>
|
|||
|
<p>A <span><i>conforming program</i></span> can control the order of processing explicitly by separating the operations into separate (possibly nested) <span><i>function forms</i></span>, or by writing explicit calls to <span><i>functions</i></span> that perform coercions.</p>
|
|||
|
<h4 class="anchor anchorWithStickyNavbar_LWe7" id="121111-examples-of-associativity-and-commutativity-in-numeric-operations">12.1.1.1.1 Examples of Associativity and Commutativity in Numeric Operations<a href="bc-b-number-concepts.html#121111-examples-of-associativity-and-commutativity-in-numeric-operations" class="hash-link" aria-label="Direct link to 12.1.1.1.1 Examples of Associativity and Commutativity in Numeric Operations" title="Direct link to 12.1.1.1.1 Examples of Associativity and Commutativity in Numeric Operations"></a></h4>
|
|||
|
<!-- -->
|
|||
|
<div class="language-lisp codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-lisp codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">Consider the following expression, in which we assume that </span><span class="token number" style="color:#36acaa">1.0</span><span class="token plain"> and 1.0e-15 both denote *single floats*: </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">+</span><span class="token plain"> 1/3 2/3 1.0d0 </span><span class="token number" style="color:#36acaa">1.0</span><span class="token plain"> 1.0e-15</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">One *conforming implementation* might process the *arguments* from left to right, first adding 1/3 and 2/3 to get 1, then converting that to a *double float* for combination with 1.0d0, then successively converting and adding </span><span class="token number" style="color:#36acaa">1.0</span><span class="token plain"> and 1.0e-15. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">Another *conforming implementation* might process the *arguments* from right to left, first performing a *single float* addition of </span><span class="token number" style="color:#36acaa">1.0</span><span class="token plain"> and 1.0e-15 </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">perhaps</span><span class="token plain"> losing accuracy in the process</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain">, then converting the sum to a *double float* and adding 1.0d0, then converting 2/3 to a *double float* and adding it, and then converting 1/3 and adding that. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">A third *conforming implementation* might first scan all the *arguments*, process all the *rationals* first to keep that part of the computation exact, then find an *argument* of the largest floating-point </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">format among all the *arguments* and add that, and then add in all other *arguments*, converting each in turn </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">all</span><span class="token plain"> in a perhaps misguided attempt to make the computation as accurate as possible</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain">. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">In any case, all three strategies are legitimate. </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">A *conforming program* could control the order by writing, for example, </span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">+</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">+</span><span class="token plain"> 1/3 2/3</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token car">+</span><span class="token plain"> 1.0d0 1.0e-15</span><span class="token punctuation" style="color:#393A34">)</span><span class="to
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="12112-contagion-in-numeric-operations">12.1.1.2 Contagion in Numeric Operations<a href="bc-b-number-concepts.html#12112-contagion-in-numeric-operations" class="hash-link" aria-label="Direct link to 12.1.1.2 Contagion in Numeric Operations" title="Direct link to 12.1.1.2 Contagion in Numeric Operations"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<p>For information about the contagion rules for implicit coercions of <span><i>arguments</i></span> in numeric operations, see Section 12.1.4.4 (Rule of Float Precision Contagion), Section 12.1.4.1 (Rule of Float and Rational Contagion), and Section 12.1.5.2 (Rule of Complex Contagion).</p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="12113-viewing-integers-as-bits-and-bytes">12.1.1.3 Viewing Integers as Bits and Bytes<a href="bc-b-number-concepts.html#12113-viewing-integers-as-bits-and-bytes" class="hash-link" aria-label="Direct link to 12.1.1.3 Viewing Integers as Bits and Bytes" title="Direct link to 12.1.1.3 Viewing Integers as Bits and Bytes"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<!-- -->
|
|||
|
<h4 class="anchor anchorWithStickyNavbar_LWe7" id="121131-logical-operations-on-integers">12.1.1.3.1 Logical Operations on Integers<a href="bc-b-number-concepts.html#121131-logical-operations-on-integers" class="hash-link" aria-label="Direct link to 12.1.1.3.1 Logical Operations on Integers" title="Direct link to 12.1.1.3.1 Logical Operations on Integers"></a></h4>
|
|||
|
<!-- -->
|
|||
|
<p>Logical operations require <em>integers</em> as arguments; an error of <span><i>type</i></span> <span><b>type-error</b></span> should be signaled if an argument is supplied that is not an <em>integer</em> . <em>Integer</em> arguments to logical operations are treated as if they were represented in two’s-complement notation.</p>
|
|||
|
<p>Figure 12–5 shows <span><i>defined names</i></span> relating to logical operations on numbers.</p>
|
|||
|
<p>|</p><p><strong>ash boole-ior logbitp</strong> </p><p><strong>boole boole-nand logcount</strong> </p><p><strong>boole-1 boole-nor logeqv</strong> </p><p><strong>boole-2 boole-orc1 logior</strong> </p><p><strong>boole-and boole-orc2 lognand</strong> </p><p><strong>boole-andc1 boole-set lognor</strong> </p><p><strong>boole-andc2 boole-xor lognot</strong> </p><p><strong>boole-c1 integer-length logorc1</strong> </p><p><strong>boole-c2 logand logorc2</strong> </p><p><strong>boole-clr logandc1 logtest</strong> </p><p><strong>boole-eqv logandc2 logxor</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–5. Defined names relating to logical operations on numbers.</strong></p>
|
|||
|
<h4 class="anchor anchorWithStickyNavbar_LWe7" id="121132-byte-operations-on-integers">12.1.1.3.2 Byte Operations on Integers<a href="bc-b-number-concepts.html#121132-byte-operations-on-integers" class="hash-link" aria-label="Direct link to 12.1.1.3.2 Byte Operations on Integers" title="Direct link to 12.1.1.3.2 Byte Operations on Integers"></a></h4>
|
|||
|
<!-- -->
|
|||
|
<p>The byte-manipulation <span><i>functions</i></span> use <span><i>objects</i></span> called <span><i>byte specifiers</i></span> to designate the size and position of a specific <span><i>byte</i></span> within an <em>integer</em> . The representation of a <span><i>byte specifier</i></span> is <em>implementation dependent</em>; it might or might not be a <span><i>number</i></span> . The <span><i>function</i></span> <span><b>byte</b></span> will construct a <span><i>byte specifier</i></span> , which various other byte-manipulation <span><i>functions</i></span> will accept.</p>
|
|||
|
<p>Figure 12–6 shows <span><i>defined names</i></span> relating to manipulating <span><i>bytes</i></span> of <span><i>numbers</i></span>.</p>
|
|||
|
<p>|</p><p><strong>byte deposit-field ldb-test</strong> </p><p><strong>byte-position dpb mask-field</strong> </p><p><strong>byte-size ldb</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–6. Defined names relating to byte manipulation.</strong></p>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="1212-implementation">12.1.2 Implementation<a href="bc-b-number-concepts.html#1212-implementation" class="hash-link" aria-label="Direct link to 12.1.2 Implementation" title="Direct link to 12.1.2 Implementation"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p>Figure 12–7 shows <span><i>defined names</i></span> relating to <span><i>implementation-dependent</i></span> details about <span><i>numbers</i></span>.</p>
|
|||
|
<p>|</p><p><strong>double-float-epsilon most-negative-fixnum</strong> </p><p><strong>double-float-negative-epsilon most-negative-long-float</strong> </p><p><strong>least-negative-double-float most-negative-short-float</strong> </p><p><strong>least-negative-long-float most-negative-single-float</strong> </p><p><strong>least-negative-short-float most-positive-double-float</strong> </p><p><strong>least-negative-single-float most-positive-fixnum</strong> </p><p><strong>least-positive-double-float most-positive-long-float</strong> </p><p><strong>least-positive-long-float most-positive-short-float</strong> </p><p><strong>least-positive-short-float most-positive-single-float</strong> </p><p><strong>least-positive-single-float short-float-epsilon</strong> </p><p><strong>long-float-epsilon short-float-negative-epsilon long-float-negative-epsilon single-float-epsilon</strong> </p><p><strong>most-negative-double-float single-float-negative-epsilon</strong></p>|<p></p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p><strong>Figure 12–7. Defined names relating to implementation-dependent details about numbers.</strong></p>
|
|||
|
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="1213-rational-computations">12.1.3 Rational Computations<a href="bc-b-number-concepts.html#1213-rational-computations" class="hash-link" aria-label="Direct link to 12.1.3 Rational Computations" title="Direct link to 12.1.3 Rational Computations"></a></h2>
|
|||
|
<!-- -->
|
|||
|
<p>The rules in this section apply to <span><i>rational</i></span> computations.</p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="12131-rule-of-unbounded-rational-precision">12.1.3.1 Rule of Unbounded Rational Precision<a href="bc-b-number-concepts.html#12131-rule-of-unbounded-rational-precision" class="hash-link" aria-label="Direct link to 12.1.3.1 Rule of Unbounded Rational Precision" title="Direct link to 12.1.3.1 Rule of Unbounded Rational Precision"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<p>Rational computations cannot overflow in the usual sense (though there may not be enough storage to represent a result), since <em>integers</em> and <span><i>ratios</i></span> may in principle be of any magnitude.</p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="12132-rule-of-canonical-representation-for-rationals">12.1.3.2 Rule of Canonical Representation for Rationals<a href="bc-b-number-concepts.html#12132-rule-of-canonical-representation-for-rationals" class="hash-link" aria-label="Direct link to 12.1.3.2 Rule of Canonical Representation for Rationals" title="Direct link to 12.1.3.2 Rule of Canonical Representation for Rationals"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<p>If any computation produces a result that is a mathematical ratio of two integers such that the denominator evenly divides the numerator, then the result is converted to the equivalent <em>integer</em> .</p>
|
|||
|
<p>If the denominator does not evenly divide the numerator, the canonical representation of a <span><i>rational</i></span> number is as the <span><i>ratio</i></span> that numerator and that denominator, where the greatest common divisor of the numerator and denominator is one, and where the denominator is positive and greater than one.</p>
|
|||
|
<p>When used as input (in the default syntax), the notation -0 always denotes the <em>integer</em> 0. A <span><i>conforming implementation</i></span> must not have a representation of “minus zero” for <em>integers</em> that is distinct from its representation of zero for <em>integers</em>. However, such a distinction is possible for <span><i>floats</i></span>; see the <span><i>type</i></span> <span><b>float</b></span>.</p>
|
|||
|
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="12133-rule-of-float-substitutability">12.1.3.3 Rule of Float Substitutability<a href="bc-b-number-concepts.html#12133-rule-of-float-substitutability" class="hash-link" aria-label="Direct link to 12.1.3.3 Rule of Float Substitutability" title="Direct link to 12.1.3.3 Rule of Float Substitutability"></a></h3>
|
|||
|
<!-- -->
|
|||
|
<p>When the arguments to an irrational mathematical <span><i>function</i></span> are all <span><i>rational</i></span> and the true mathe matical result is also (mathematically) rational, then unless otherwise noted an implementation is free to return either an accurate <span><i>rational</i></span> result or a <span><i>single float</i></span> approximation. If the arguments</p>
|
|||
|
<p>are all <span><i>rational</i></span> but the result cannot be expressed as a <span><i>rational</i></span> number, then a <span><i>single float</i></span> approximation is always returned.</p>
|
|||
|
<p>If the arguments to an irrational mathematical <span><i>function</i></span> are all of type</p>
|
|||
|
<p>(or rational (complex rational)) and the true mathematical result is (mathematically) a complex number with rational real and imaginary parts, then unless otherwise noted an imple mentation is free to return either an accurate result of type (or rational (complex rational)) or a <span><i>single float</i></span> (permissible only if the imaginary part of the true mathematical result is zero) or (complex single-float). If the arguments are all of type (or rational (complex rational)) but the result cannot be expressed as a <span><i>rational</i></span> or <span><i>complex rational</i></span>, then the returned value will be of <span><i>type</i></span> <span><b>single-float</b></span> (permissible only if the imaginary part of the true mathematical result is zero) or (complex single-float).</p>
|
|||
|
<p>Float substitutability applies neither to the rational <span><i>functions</i></span> <span><b>+</b></span>, <span><b>-</b></span>, <strong>*</strong>, and <span><b>/</b></span> nor to the related <span><i>operators</i></span> <span><b>1+</b></span>, <strong>1-</strong>, <span><b>incf</b></span>, <span><b>decf</b></span>, and <span><b>conjugate</b></span>. For rational <span><i>functions</i></span>, if all arguments are <span><i>rational</i></span>, then the result is <span><i>rational</i></span>; if all arguments are of type (or rational (complex rational)), then the result is of type (or rational (complex rational)).</p>
|
|||
|
<p>|<strong>Function Sample Results</strong>|</p>
|
|||
|
<p>| :- |</p>
|
|||
|
<p>|</p><p><span><b>abs</b></span> (abs #c(3 4)) → 5 <em>or</em> 5.0 </p><p><span><b>acos</b></span> (acos 1) |