<p>Starting with version 4.5, <strong class="pkg">GAP</strong> has built-in support for floating-point numbers in machine format, and allows package to implement arbitrary-precision floating-point arithmetic in a uniform manner. For now, one such package, <strong class="pkg">Float</strong> exists, and is based on the arbitrary-precision routines in <strong class="pkg">mpfr</strong>.</p>
<p>A word of caution: <strong class="pkg">GAP</strong> deals primarily with algebraic objects, which can be represented exactly in a computer. Numerical imprecision means that floating-point numbers do not form a ring in the strict <strong class="pkg">GAP</strong> sense, because addition is in general not associative (<code class="code">(1.0e-100+1.0)-1.0</code> is not the same as <code class="code">1.0e-100+(1.0-1.0)</code>, in the default precision setting).</p>
<p>Most algorithms in <strong class="pkg">GAP</strong> which require ring elements will therefore not be applicable to floating-point elements. In some cases, such a notion would not even make any sense (what is the greatest common divisor of two floating-point numbers?)</p>
<p>Floating-point numbers can also be created using <code class="code">Float</code>, from strings or rational numbers; and can be converted back using <code class="code">String,Rat,Int</code>.</p>
<p><strong class="pkg">GAP</strong> allows rational and floating-point numbers to be mixed in the elementary operations <code class="code">+,-,*,/</code>. However, floating-point numbers and rational numbers may not be compared. Conversions are performed using the creator <code class="code">Float</code>:</p>
<p>Floating-point numbers may be directly input, as in any usual mathematical software or language; with the exception that every floating-point number must contain a decimal digit. Therefore <code class="code">.1</code>, <code class="code">.1e1</code>, <code class="code">-.999</code> etc. are all valid <strong class="pkg">GAP</strong> inputs.</p>
<p>Floating-point numbers so entered in <strong class="pkg">GAP</strong> are stored as strings. They are converted to floating-point when they are first used. This means that, if the floating-point precision is increased, the constants are reevaluated to fit the new format.</p>
<p>Floating-point numbers may be followed by an underscore, as in <code class="code">1._</code>. This means that they are to be immediately converted to the current floating-point format. The underscore may be followed by a single letter, which specifies which format/precision to use. By default, <strong class="pkg">GAP</strong> has a single floating-point handler, with fixed (53 bits) precision, and its format specifier is <code class="code">'l'</code> as in <code class="code">1._l</code>. Higher-precision floating-point computations is available via external packages; <strong class="pkg">float</strong> for example.</p>
<p>A record, <code class="func">FLOAT</code> (<a href="chap19.html#X819050BF8403806E"><span class="RefLink">19.2-5</span></a>), contains all relevant constants for the current floating-point format; see its documentation for details. Typical fields are <code class="code">FLOAT.MANT_DIG=53</code>, the constant <code class="code">FLOAT.VIEW_DIG=6</code> specifying the number of digits to view, and <code class="code">FLOAT.PI</code> for the constant <span class="SimpleMath">π</span>. The constants have the same name as their C counterparts, except for the missing initial <code class="code">DBL_</code> or <code class="code">M_</code>.</p>
<p>Floating-point numbers may be created using the single function <code class="func">Float</code> (<a href="chap19.html#X86D5EA93813FB6C4"><span class="RefLink">19.2-1</span></a>), which accepts as arguments rational, string, or floating-point numbers. Floating-point numbers may also be created, in any floating-point representation, using <code class="func">NewFloat</code> (<a href="chap19.html#X86D5EA93813FB6C4"><span class="RefLink">19.2-1</span></a>) as in <code class="code">NewFloat(IsIEEE754FloatRep,355/113)</code>, by supplying the category filter of the desired new floating-point number; or using <code class="func">MakeFloat</code> (<a href="chap19.html#X86D5EA93813FB6C4"><span class="RefLink">19.2-1</span></a>) as in <code class="code">MakeFloat(1.0,355/113)</code>, by supplying a sample floating-point number.</p>
<p>Floating-point numbers may also be converted to other <strong class="pkg">GAP</strong> formats using the usual commands <code class="func">Int</code> (<a href="chap14.html#X87CA734380B5F68C"><span class="RefLink">14.2-3</span></a>), <code class="func">Rat</code> (<a href="chap17.html#X7EB4C646806A2BDE"><span class="RefLink">17.2-6</span></a>), <code class="func">String</code> (<a href="chap27.html#X81FB5BE27903EC32"><span class="RefLink">27.7-6</span></a>).</p>
<p>Exact conversion to and from floating-point format may be done using external representations. The "external representation" of a floating-point number <code class="code">x</code> is a pair <code class="code">[m,e]</code> of integers, such that <code class="code">x=m*2^(-1+e-LogInt(AbsInt(m),2))</code>. Conversion to and from external representation is performed as usual using <code class="func">ExtRepOfObj</code> (<a href="chap79.html#X8542B32A8206118C"><span class="RefLink">79.8-1</span></a>) and <code class="func">ObjByExtRep</code> (<a href="chap79.html#X8542B32A8206118C"><span class="RefLink">79.8-1</span></a>):</p>
<p>Computations with floating-point numbers never raise any error. Division by zero is allowed, and produces a signed infinity. Illegal operations, such as <code class="code">0./0.</code>, produce <code class="keyw">NaN</code>'s (not-a-number); this is the only floating-point number x such that not EqFloat(x+0.0,x).
<p>The IEEE754 standard requires <code class="keyw">NaN</code> to be non-equal to itself. On the other hand, <strong class="pkg">GAP</strong> requires every object to be equal to itself. To respect the IEEE754 standard, the function <code class="func">EqFloat</code> (<a href="chap19.html#X7BD96E0585D5A1EE"><span class="RefLink">19.2-6</span></a>) should be used instead of <code class="code">=</code>.</p>
<p>The category a floating-point belongs to can be checked using the filters <code class="func">IsFinite</code> (<a href="chap30.html#X808A4061809A6E67"><span class="RefLink">30.4-2</span></a>), <code class="func">IsPInfinity</code> (<a href="chap19.html#X7E03FDEE824D1E8E"><span class="RefLink">19.2-13</span></a>), <code class="func">IsNInfinity</code> (<a href="chap19.html#X7E03FDEE824D1E8E"><span class="RefLink">19.2-13</span></a>), <code class="func">IsXInfinity</code> (<a href="chap19.html#X7E03FDEE824D1E8E"><span class="RefLink">19.2-13</span></a>), <code class="func">IsNaN</code> (<a href="chap19.html#X7E03FDEE824D1E8E"><span class="RefLink">19.2-13</span></a>).</p>
<p>Comparisons between floating-point numbers and rationals are explicitly forbidden. The rationale is that objects belonging to different families should in general not be comparable in <strong class="pkg">GAP</strong>. Floating-point numbers are also approximations of real numbers, and don't follow the same rules; consider for example, using the default GAP implementation of floating-point numbers,
<p>This function creates a new floating-point number.</p>
<p>If <var class="Arg">obj</var> is a rational number, the created number is created with sufficient precision so that the number can (usually) be converted back to the original number (see <code class="func">Rat</code> (<span class="RefLink">Reference: Rat</span>) and <code class="func">Rat</code> (<a href="chap17.html#X7EB4C646806A2BDE"><span class="RefLink">17.2-6</span></a>)). For an integer, the precision, if unspecified, is chosen sufficient so that <code class="code">Int(Float(obj))=obj</code> always holds, but at least 64 bits.</p>
<p><var class="Arg">obj</var> may also be a string, which may be of the form <code class="code">"3.14e0"</code> or <code class="code">".314e1"</code> or <code class="code">".314@1"</code> etc.</p>
<p>An option may be passed to specify, it bits, a desired precision. The format is <code class="code">Float("3.14":PrecisionFloat:=1000)</code> to create a 1000-bit approximation of <span class="SimpleMath">3.14</span>.</p>
<p>In particular, if <var class="Arg">obj</var> is already a floating-point number, then <code class="code">Float(obj:PrecisionFloat:=prec)</code> creates a copy of <var class="Arg">obj</var> with a new precision. prec</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ Rat</code>( <var class="Arg">f</var> )</td><td class="tdright">( attribute )</td></tr></table></div>
<p>Returns: A rational approximation to <var class="Arg">f</var></p>
<p>This command constructs a rational approximation to the floating-point number <var class="Arg">f</var>. Of course, it is not guaranteed to return the original rational number <var class="Arg">f</var> was created from, though it returns the most `reasonable' one given the precision of f.
<p>Two options control the precision of the rational approximation: In the form <code class="code">Rat(f:maxdenom:=md,maxpartial:=mp)</code>, the rational returned is such that the denominator is at most <var class="Arg">md</var> and the partials in its continued fraction expansion are at most <var class="Arg">mp</var>. The default values are <code class="code">maxpartial:=10000</code> and <code class="code">maxdenom:=2^(precision/2)</code>.</p>
<p>This command constructs a cyclotomic approximation to the floating-point number <var class="Arg">f</var>. Of course, it is not guaranteed to return the original rational number <var class="Arg">f</var> was created from, though it returns the most `reasonable' one given the precision of f. An optional argument degree specifies the maximal degree of the cyclotomic to be constructed.
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ SetFloats</code>( <var class="Arg">rec</var>[, <var class="Arg">bits</var>][, <var class="Arg">install</var>] )</td><td class="tdright">( function )</td></tr></table></div>
<p>Installs a new interface to floating-point numbers in <strong class="pkg">GAP</strong>, optionally with a desired precision <var class="Arg">bits</var> in binary digits. The last optional argument <var class="Arg">install</var> is a boolean value; if false, it only installs the eager handler and the precision for the floateans, without making them the default.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ EqFloat</code>( <var class="Arg">x</var>, <var class="Arg">y</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>Returns: Whether the floateans <var class="Arg">x</var> and <var class="Arg">y</var> are equal</p>
<p>This function compares two floating-point numbers, and returns <code class="keyw">true</code> if they are equal, and <code class="keyw">false</code> otherwise; with the exception that <code class="keyw">NaN</code> is always considered to be different from itself.</p>
<p>The first function <code class="code">SignBit</code> returns the sign bit of the floating-point number <var class="Arg">x</var>: <code class="keyw">true</code> if <var class="Arg">x</var> is negative (including <code class="code">-0.</code>) and <code class="keyw">false</code> otherwise.</p>
<p>The second function <code class="code">SignFloat</code> returns the integer <code class="keyw">-1</code> if <var class="Arg">x<0</var>, <code class="keyw">0</code> if <var class="Arg">x=0</var> and <code class="keyw">1</code> if <var class="Arg">x>0</var>.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ Atan2</code>( <var class="Arg">y</var>, <var class="Arg">x</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>Returns: The polar angle of <var class="Arg">(x, y)</var> in the plane as float.</p>
<p>Returns the principal value of the argument (polar angle) of <span class="SimpleMath">(<var class="Arg">x</var>, <var class="Arg">y</var>)</span> in the plane. The returned value will always be in <span class="SimpleMath">(-π , π]</span> and is not defined on <span class="SimpleMath">(0,0)</span>. This function is defined in accordance with IEEE 1788-2015 and imported from IEEE 754.</p>
<p>The first function <code class="code">Log1p</code> returns the natural logarithm <span class="SimpleMath">log(<var class="Arg">x</var>+1)</span>.</p>
<p>The second function <code class="code">Expm1</code> returns the exponential function <span class="SimpleMath">exp(<var class="Arg">x</var>)-1</span></p>
<p>These two functions are inverse to each other.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ Erf</code>( <var class="Arg">x</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>Returns: The error function given by the Gaussian integral</p>
<p>Returns the error function imported from IEEE 754 given by the formula:</p>
<p><strong class="pkg">GAP</strong> provides a mechanism for packages to implement new floating-point numerical interfaces. The following describes that mechanism, actual examples of packages are documented separately.</p>
<p>A package must create a record with fields (all optional)</p>
<dl>
<dt><strong class="Mark">creator</strong></dt>
<dd><p>a function converting strings to floating-point;</p>
</dd>
<dt><strong class="Mark">eager</strong></dt>
<dd><p>a character allowing immediate conversion to floating-point;</p>
</dd>
<dt><strong class="Mark">objbyextrep</strong></dt>
<dd><p>a function creating a floating-point number out of a list <code class="code">[mantissa,exponent]</code>;</p>
</dd>
<dt><strong class="Mark">filter</strong></dt>
<dd><p>a filter for the new floating-point objects;</p>
</dd>
<dt><strong class="Mark">constants</strong></dt>
<dd><p>a record containing numerical constants, such as <code class="code">MANT_DIG</code>, <code class="code">MAX</code>, <code class="code">MIN</code>, <code class="code">NAN</code>.</p>
</dd>
</dl>
<p>The package must install methods <code class="code">Int</code>, <code class="code">Rat</code>, <code class="code">String</code> for its objects, and creators <code class="code">NewFloat(filter,IsRat)</code>, <code class="code">NewFloat(IsString)</code>.</p>
<p>It must then install methods for all arithmetic and numerical operations: <code class="code">SUM</code>, <code class="code">Exp</code>, ...</p>
<p>The user chooses that implementation by calling <code class="func">SetFloats</code> (<a href="chap19.html#X7A962B0983FA66E8"><span class="RefLink">19.2-4</span></a>) with the record as argument, and with an optional second argument requesting a precision in binary digits.</p>
<p>Complex arithmetic may be implemented in packages, and is present in <strong class="pkg">float</strong>. Complex numbers are treated as usual numbers; they may be input with an extra "i" as in <code class="code">-0.5+0.866i</code>. They may also be created using <code class="func">NewFloat</code> (<a href="chap19.html#X86D5EA93813FB6C4"><span class="RefLink">19.2-1</span></a>) with three arguments: the float filter, the real part, and the imaginary part.</p>
<p>Methods should then be implemented for <code class="code">Norm</code>, <code class="code">RealPart</code>, <code class="code">ImaginaryPart</code>, <code class="code">ComplexConjugate</code>, ...</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ Argument</code>( <var class="Arg">z</var> )</td><td class="tdright">( attribute )</td></tr></table></div>
<p>Returns the argument of the complex number <var class="Arg">z</var>, namely the value <code class="code">Atan2(ImaginaryPart(z),RealPart(z))</code>.</p>
<p>Interval arithmetic may also be implemented in packages. Intervals are in fact efficient implementations of sets of real numbers. The only non-trivial issue is how they should be compared. The standard <code class="code">EQ</code> tests if the intervals are equal; however, it is usually more useful to know if intervals overlap, or are disjoint, or are contained in each other.</p>
<p>Note the usual convention that intervals are compared as in <span class="SimpleMath">[a,b]≤[c,d]</span> if and only if <span class="SimpleMath">a≤ c</span> and <span class="SimpleMath">b≤ d</span>.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ BisectInterval</code>( <var class="Arg">x</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>Returns a list of two intervals whose union equals the interval <var class="Arg">x</var>.</p>
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.