NumericAnnex/docsets/NumericAnnex.docset/Contents/Resources/Documents/Structs.html

305 lines
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Structs Reference</title>
<link rel="stylesheet" type="text/css" href="css/highlight.css">
<link rel="stylesheet" type="text/css" href="css/jazzy.css">
<script src="js/jquery.min.js" defer></script>
<script src="js/jazzy.js" defer></script>
</head>
<body>
<a title="Structs Reference"></a>
<header>
<p class="primary">
<a href="index.html">NumericAnnex Reference</a>
(100% documented)
</p>
<p>
<a href="https://github.com/xwu/NumericAnnex">View on GitHub</a>
</p>
<p>
<a href="dash-feed://https%3A%2F%2Fxwu%2Egithub%2Eio%2FNumericAnnex%2Fdocsets%2FNumericAnnex%2Exml">Install in Dash</a>
</p>
</header>
<div class="wrapper">
<nav>
<ul>
<li>
<p><a href="Classes.html">Classes</a></p>
<ul>
<li>
<a href="Classes/Random.html">Random</a>
</li>
<li>
<a href="Classes/Random/Xoroshiro.html"> Xoroshiro</a>
</li>
</ul>
</li>
<li>
<p><a href="Extensions.html">Extensions</a></p>
<ul>
<li>
<a href="Extensions/BinaryInteger.html">BinaryInteger</a>
</li>
<li>
<a href="Extensions/Double.html">Double</a>
</li>
<li>
<a href="Extensions/Float.html">Float</a>
</li>
<li>
<a href="Extensions/FloatingPoint.html">FloatingPoint</a>
</li>
<li>
<a href="Extensions/UnsignedInteger.html">UnsignedInteger</a>
</li>
</ul>
</li>
<li>
<p><a href="Functions.html">Functions</a></p>
<ul>
<li>
<a href="Functions.html#/s:12NumericAnnex3absAA7ComplexVyxGAeA4RealRzs9DecodableRzs9EncodableRzs33_ExpressibleByBuiltinFloatLiteralRzlF">abs(_:)</a>
</li>
<li>
<a href="Functions.html#/s:12NumericAnnex3absAA8RationalVyxGAEs9DecodableRzs9EncodableRzs13SignedIntegerRzs021_ExpressibleByBuiltinH7LiteralRzs08UnsignedH09Magnitudes0A0PRpzAkM_AKQZANRSlF">abs(_:)</a>
</li>
<li>
<a href="Functions.html#/s:12NumericAnnex4ceilAA8RationalVyxGAEs9DecodableRzs9EncodableRzs13SignedIntegerRzs021_ExpressibleByBuiltinH7LiteralRzs08UnsignedH09Magnitudes0A0PRpzAkM_AKQZANRSlF">ceil(_:)</a>
</li>
<li>
<a href="Functions.html#/s:12NumericAnnex5floorAA8RationalVyxGAEs9DecodableRzs9EncodableRzs13SignedIntegerRzs021_ExpressibleByBuiltinH7LiteralRzs08UnsignedH09Magnitudes0A0PRpzAkM_AKQZANRSlF">floor(_:)</a>
</li>
<li>
<a href="Functions.html#/s:12NumericAnnex5roundAA8RationalVyxGAEs9DecodableRzs9EncodableRzs13SignedIntegerRzs021_ExpressibleByBuiltinH7LiteralRzs08UnsignedH09Magnitudes0A0PRpzAkM_AKQZANRSlF">round(_:)</a>
</li>
<li>
<a href="Functions.html#/s:12NumericAnnex4sqrtAA7ComplexVyxGAeA4RealRzs9DecodableRzs9EncodableRzs33_ExpressibleByBuiltinFloatLiteralRzlF">sqrt(_:)</a>
</li>
<li>
<a href="Functions.html#/s:12NumericAnnex5truncAA8RationalVyxGAEs9DecodableRzs9EncodableRzs13SignedIntegerRzs021_ExpressibleByBuiltinH7LiteralRzs08UnsignedH09Magnitudes0A0PRpzAkM_AKQZANRSlF">trunc(_:)</a>
</li>
</ul>
</li>
<li>
<p><a href="Protocols.html">Protocols</a></p>
<ul>
<li>
<a href="Protocols/Math.html">Math</a>
</li>
<li>
<a href="Protocols/PRNG.html">PRNG</a>
</li>
<li>
<a href="Protocols/Real.html">Real</a>
</li>
</ul>
</li>
<li>
<p><a href="Structs.html">Structs</a></p>
<ul>
<li>
<a href="Structs/Complex.html">Complex</a>
</li>
<li>
<a href="Structs/Rational.html">Rational</a>
</li>
</ul>
</li>
<li>
<p><a href="Typealiases.html">Typealiases</a></p>
<ul>
<li>
<a href="Typealiases.html#/s:12NumericAnnex12RoundingRulea">RoundingRule</a>
</li>
<li>
<a href="Typealiases.html#/s:12NumericAnnex4Signa">Sign</a>
</li>
</ul>
</li>
</ul>
</nav>
<article>
<section>
<div class="wrapper">
<div class="overview">
<h1>Structs</h1>
<p>The following structs are available globally.</p>
</div>
</div>
</section>
<section class="toc">
<details open>
<summary>Topics</summary>
<div class="wrapper">
<ul>
<li><a href="#/s:12NumericAnnex7ComplexV">Complex</a></li>
</ul>
<ul>
<li><a href="#/s:12NumericAnnex8RationalV">Rational</a></li>
</ul>
</div>
</details>
</section>
<section>
<div class="wrapper">
<div class="group">
<ul>
<li>
<div>
<a name="/s:12NumericAnnex7ComplexV"></a>
<a name="//apple_ref/swift/Struct/Complex" class="dashAnchor"></a>
<h4>
<code>Complex</code>
</h4>
</div>
<div class="abstract">
<p>A type to represent a complex value in Cartesian form.</p>
<div class="aside aside-note">
<p class="aside-title">Note</p>
<code>Complex64</code> is a type alias for <code>Complex&lt;Float&gt;</code> and <code>Complex128</code> is
a type alias for <code>Complex&lt;Double&gt;</code>.
</div>
<p>Create new instances of <code>Complex&lt;T&gt;</code> using integer or floating-point
literals and the imaginary unit <code><a href="Structs/Complex.html#/s:12NumericAnnex7ComplexV1iACyxGvZ">Complex&lt;T&gt;.i</a></code>. For example:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="o">.</span><span class="n">i</span> <span class="c1">// `x` is of type `Complex&lt;Double&gt;`</span>
<span class="k">let</span> <span class="nv">y</span> <span class="o">=</span> <span class="mf">3.5</span> <span class="o">+</span> <span class="mi">7</span> <span class="o">*</span> <span class="o">.</span><span class="n">i</span> <span class="c1">// `y` is of type `Complex&lt;Double&gt;`</span>
<span class="k">let</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Complex64</span> <span class="o">=</span> <span class="o">.</span><span class="n">e</span> <span class="o">+</span> <span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="o">.</span><span class="n">i</span> <span class="c1">// `z` is of type `Complex&lt;Float&gt;`</span>
</code></pre>
<h2 id='additional-considerations' class='heading'>Additional Considerations</h2>
<p>Floating-point types have special values that represent infinity or NaN
(<q>not a number</q>). Complex functions in different languages may return
different results when working with special values.</p>
<p>Many complex functions have <a href="http://mathworld.wolfram.com/BranchCut.html">branch cuts</a>, which are curves in the
complex plane across which a function is discontinuous. Different languages
may adopt different branch cut structures for the same complex function.</p>
<p>Implementations in <code>Complex&lt;T&gt;</code> adhere to the <a href="http://www.open-std.org/JTC1/SC22/WG14/www/standards.html#9899">C standard</a> (Annex G) as
closely as possible with respect to special values and branch cuts.</p>
<p>To users unfamiliar with complex functions, the principal value returned by
some complex functions may be unexpected. For example,
<code>Double.cbrt(-8) == -2</code>, which is the <strong>real root</strong>, while
<code>Complex.cbrt(-8) == 2 * Complex.exp(.i * .pi / 3)</code>, which is the
<strong>principal root</strong>.</p>
<a href="Structs/Complex.html">See more…</a>
</div>
<div class="declaration">
<h5>Declaration</h5>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="kd">struct</span> <span class="kt">Complex</span><span class="o">&lt;</span><span class="kt">T</span> <span class="p">:</span> <span class="kt"><a href="Protocols/Real.html">Real</a></span><span class="o">&gt;</span> <span class="p">:</span> <span class="kt">Codable</span>
<span class="k">where</span> <span class="kt">T</span> <span class="p">:</span> <span class="kt">Codable</span> <span class="o">&amp;</span> <span class="n">_ExpressibleByBuiltinFloatLiteral</span></code></pre>
</div>
</div>
</li>
</ul>
</div>
<div class="group">
<ul>
<li>
<div>
<a name="/s:12NumericAnnex8RationalV"></a>
<a name="//apple_ref/swift/Struct/Rational" class="dashAnchor"></a>
<h4>
<code>Rational</code>
</h4>
</div>
<div class="abstract">
<p>A type to represent a rational value.</p>
<div class="aside aside-note">
<p class="aside-title">Note</p>
<code>Ratio</code> is a type alias for <code>Rational&lt;Int&gt;</code>.
</div>
<p>Create new instances of <code>Rational&lt;T&gt;</code> by using integer literals and the
division (<code>/</code>) operator. For example:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">x</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="mi">3</span> <span class="k">as</span> <span class="kt">Ratio</span> <span class="c1">// `x` is of type `Rational&lt;Int&gt;`</span>
<span class="k">let</span> <span class="nv">y</span> <span class="o">=</span> <span class="mi">2</span> <span class="k">as</span> <span class="kt">Ratio</span> <span class="c1">// `y` is of type `Rational&lt;Int&gt;`</span>
<span class="k">let</span> <span class="nv">z</span><span class="p">:</span> <span class="kt">Ratio</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">/</span> <span class="mi">3</span> <span class="c1">// `z` is also of type `Rational&lt;Int&gt;`</span>
<span class="nf">print</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">)</span> <span class="c1">// Prints "3"</span>
</code></pre>
<p>You can create an unreduced fraction by using the initializer
<code><a href="Structs/Rational.html#/s:12NumericAnnex8RationalVACyxGx9numerator_x11denominatortcfc">Rational&lt;T&gt;.init(numerator:denominator:)</a></code>. For example:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">a</span> <span class="o">=</span> <span class="kt">Ratio</span><span class="p">(</span><span class="nv">numerator</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="nv">denominator</span><span class="p">:</span> <span class="mi">3</span><span class="p">)</span>
<span class="nf">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c1">// Prints "3/3"</span>
</code></pre>
<p>All arithmetic operations on values in canonical form (i.e. reduced to
lowest terms) return results in canonical form. However, operations on
values not in canonical form may or may not return results that are
themselves in canonical form. The property <code>canonicalized</code> is the canonical
form of any value.</p>
<h2 id='additional-considerations' class='heading'>Additional Considerations</h2>
<h3 id='special-values' class='heading'>Special Values</h3>
<p><code>Rational&lt;T&gt;</code> does not prohibit zero as a denominator. Any instance with a
positive numerator and zero denominator represents (positive) infinity; any
instance with a negative numerator and zero denominator represents negative
infinity; and any instance with zero numerator and zero denominator
represents NaN (<q>not a number</q>).</p>
<p>As with floating-point types, <code><a href="Structs/Rational.html#/s:12NumericAnnex8RationalV8infinityACyxGvZ">Rational&lt;T&gt;.infinity</a></code> compares greater than
every finite value and negative infinity, and <code>-Rational&lt;T&gt;.infinity</code>
compares less than every finite value and positive infinity. Infinite values
of the same sign compare equal to each other.</p>
<p>As with floating-point types, <code><a href="Structs/Rational.html#/s:12NumericAnnex8RationalV3nanACyxGvZ">Rational&lt;T&gt;.nan</a></code> does not compare equal to
any other value, including another NaN. Use the property <code>isNaN</code> to test if
a value is NaN. <code>Rational&lt;T&gt;</code> arithmetic operations are intended to
propagate NaN in the same manner as analogous floating-point operations.</p>
<h3 id='numerical-limits' class='heading'>Numerical Limits</h3>
<p>When a value of type <code>Rational&lt;T&gt;</code> is in canonical form, the sign of the
numerator is the sign of the value; that is, in canonical form, the sign of
the denominator is always positive. Therefore, <code>-1 / T.min</code> cannot be
represented as a value of type <code>Rational&lt;T&gt;</code> because <code>abs(T.min)</code> cannot be
represented as a value of type <code>T</code>.</p>
<p>To ensure that every representable value of type <code>Rational&lt;T&gt;</code> has a
representable magnitude and reciprocal of the same type, an overflow trap
occurs when the division (<code>/</code>) operator is used to create a value of type
<code>Rational&lt;T&gt;</code> with numerator <code>T.min</code>.</p>
<a href="Structs/Rational.html">See more…</a>
</div>
<div class="declaration">
<h5>Declaration</h5>
<div class="language">
<p class="aside-title">Swift</p>
<pre class="highlight"><code><span class="kd">public</span> <span class="kd">struct</span> <span class="kt">Rational</span><span class="o">&lt;</span><span class="kt">T</span> <span class="p">:</span> <span class="kt">SignedInteger</span><span class="o">&gt;</span> <span class="p">:</span> <span class="kt">Codable</span>
<span class="k">where</span> <span class="kt">T</span> <span class="p">:</span> <span class="kt">Codable</span> <span class="o">&amp;</span> <span class="n">_ExpressibleByBuiltinIntegerLiteral</span><span class="p">,</span>
<span class="kt">T</span><span class="o">.</span><span class="kt">Magnitude</span> <span class="p">:</span> <span class="kt">UnsignedInteger</span><span class="p">,</span>
<span class="kt">T</span><span class="o">.</span><span class="kt">Magnitude</span><span class="o">.</span><span class="kt">Magnitude</span> <span class="o">==</span> <span class="kt">T</span><span class="o">.</span><span class="kt">Magnitude</span></code></pre>
</div>
</div>
</li>
</ul>
</div>
</div>
</section>
</article>
</div>
<footer>
<p>&copy; 2017 <a class="link" href="https://diploid.ca" target="_blank" rel="external">Xiaodi Wu</a>. All rights reserved. (Last updated: 2017-08-12)</p>
<p>Generated by <a href="https://github.com/realm/jazzy" target="_blank" rel="external">Jazzy</a> v0.8.3, a <a href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</footer>
</body>
</div>
</html>