📄 bigfloat.html
字号:
number.</p>
<p>Please note that ':constant' does not affect integer constants, nor binary
nor hexadecimal constants. Use <a href="../../lib/bignum.html">the bignum manpage</a> or <a href="../../lib/Math/BigInt.html">the Math::BigInt manpage</a> to get this to
work.</p>
<p>
</p>
<h2><a name="math_library">Math library</a></h2>
<p>Math with the numbers is done (by default) by a module called
Math::BigInt::Calc. This is equivalent to saying:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Calc'</span><span class="operator">;</span>
</pre>
<p>You can change this by using:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'BitVect'</span><span class="operator">;</span>
</pre>
<p>The following would first try to find Math::BigInt::Foo, then
Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Foo,Math::BigInt::Bar'</span><span class="operator">;</span>
</pre>
<p>Calc.pm uses as internal format an array of elements of some decimal base
(usually 1e7, but this might be differen for some systems) with the least
significant digit first, while BitVect.pm uses a bit vector of base 2, most
significant bit first. Other modules might use even different means of
representing the numbers. See the respective module documentation for further
details.</p>
<p>Please note that Math::BigFloat does <strong>not</strong> use the denoted library itself,
but it merely passes the lib argument to Math::BigInt. So, instead of the need
to do:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Math::BigInt</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'GMP'</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
</pre>
<p>you can roll it all into one line:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'GMP'</span><span class="operator">;</span>
</pre>
<p>It is also possible to just require Math::BigFloat:</p>
<pre>
<span class="keyword">require</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
</pre>
<p>This will load the neccessary things (like BigInt) when they are needed, and
automatically.</p>
<p>Use the lib, Luke! And see <a href="#using_math__bigint__lite">Using Math::BigInt::Lite</a> for more details than
you ever wanted to know about loading a different library.</p>
<p>
</p>
<h2><a name="using_math__bigint__lite">Using Math::BigInt::Lite</a></h2>
<p>It is possible to use <a href="../../Math/BigInt/Lite.html">the Math::BigInt::Lite manpage</a> with Math::BigFloat:</p>
<pre>
<span class="comment"># 1</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">with</span> <span class="operator">=></span> <span class="string">'Math::BigInt::Lite'</span><span class="operator">;</span>
</pre>
<p>There is no need to "use Math::BigInt" or "use Math::BigInt::Lite", but you
can combine these if you want. For instance, you may want to use
Math::BigInt objects in your main script, too.</p>
<pre>
<span class="comment"># 2</span>
<span class="keyword">use</span> <span class="variable">Math::BigInt</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">with</span> <span class="operator">=></span> <span class="string">'Math::BigInt::Lite'</span><span class="operator">;</span>
</pre>
<p>Of course, you can combine this with the <code>lib</code> parameter.</p>
<pre>
<span class="comment"># 3</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">with</span> <span class="operator">=></span> <span class="string">'Math::BigInt::Lite'</span><span class="operator">,</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'GMP,Pari'</span><span class="operator">;</span>
</pre>
<p>There is no need for a "use Math::BigInt;" statement, even if you want to
use Math::BigInt's, since Math::BigFloat will needs Math::BigInt and thus
always loads it. But if you add it, add it <strong>before</strong>:</p>
<pre>
<span class="comment"># 4</span>
<span class="keyword">use</span> <span class="variable">Math::BigInt</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">with</span> <span class="operator">=></span> <span class="string">'Math::BigInt::Lite'</span><span class="operator">,</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'GMP,Pari'</span><span class="operator">;</span>
</pre>
<p>Notice that the module with the last <code>lib</code> will "win" and thus
it's lib will be used if the lib is available:</p>
<pre>
<span class="comment"># 5</span>
<span class="keyword">use</span> <span class="variable">Math::BigInt</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Bar,Baz'</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">with</span> <span class="operator">=></span> <span class="string">'Math::BigInt::Lite'</span><span class="operator">,</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Foo'</span><span class="operator">;</span>
</pre>
<p>That would try to load Foo, Bar, Baz and Calc (in that order). Or in other
words, Math::BigFloat will try to retain previously loaded libs when you
don't specify it onem but if you specify one, it will try to load them.</p>
<p>Actually, the lib loading order would be "Bar,Baz,Calc", and then
"Foo,Bar,Baz,Calc", but independend of which lib exists, the result is the
same as trying the latter load alone, except for the fact that one of Bar or
Baz might be loaded needlessly in an intermidiate step (and thus hang around
and waste memory). If neither Bar nor Baz exist (or don't work/compile), they
will still be tried to be loaded, but this is not as time/memory consuming as
actually loading one of them. Still, this type of usage is not recommended due
to these issues.</p>
<p>The old way (loading the lib only in BigInt) still works though:</p>
<pre>
<span class="comment"># 6</span>
<span class="keyword">use</span> <span class="variable">Math::BigInt</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Bar,Baz'</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
</pre>
<p>You can even load Math::BigInt afterwards:</p>
<pre>
<span class="comment"># 7</span>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">Math::BigInt</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Bar,Baz'</span><span class="operator">;</span>
</pre>
<p>But this has the same problems like #5, it will first load Calc
(Math::BigFloat needs Math::BigInt and thus loads it) and then later Bar or
Baz, depending on which of them works and is usable/loadable. Since this
loads Calc unnecc., it is not recommended.</p>
<p>Since it also possible to just require Math::BigFloat, this poses the question
about what libary this will use:</p>
<pre>
<span class="keyword">require</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="variable">Math::BigFloat</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(</span><span class="number">123</span><span class="operator">);</span> <span class="variable">$x</span> <span class="operator">+=</span> <span class="number">123</span><span class="operator">;</span>
</pre>
<p>It will use Calc. Please note that the call to <a href="../../lib/Pod/perlfunc.html#item_import"><code>import()</code></a> is still done, but
only when you use for the first time some Math::BigFloat math (it is triggered
via any constructor, so the first time you create a Math::BigFloat, the load
will happen in the background). This means:</p>
<pre>
<span class="keyword">require</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
<span class="variable">Math::BigFloat</span><span class="operator">-></span><span class="variable">import</span> <span class="operator">(</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Foo,Bar'</span> <span class="operator">);</span>
</pre>
<p>would be the same as:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Math::BigFloat</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Foo, Bar'</span><span class="operator">;</span>
</pre>
<p>But don't try to be clever to insert some operations in between:</p>
<pre>
<span class="keyword">require</span> <span class="variable">Math::BigFloat</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="variable">Math::BigFloat</span><span class="operator">-></span><span class="variable">bone</span><span class="operator">()</span> <span class="operator">+</span> <span class="number">4</span><span class="operator">;</span> <span class="comment"># load BigInt and Calc</span>
<span class="variable">Math::BigFloat</span><span class="operator">-></span><span class="variable">import</span><span class="operator">(</span> <span class="string">lib</span> <span class="operator">=></span> <span class="string">'Pari'</span> <span class="operator">);</span> <span class="comment"># load Pari, too</span>
<span class="variable">$x</span> <span class="operator">=</span> <span class="variable">Math::BigFloat</span><span class="operator">-></span><span class="variable">bone</span><span class="operator">()+</span><span class="number">4</span><span class="operator">;</span> <span class="comment"># now use Pari</span>
</pre>
<p>While this works, it loads Calc needlessly. But maybe you just wanted that?</p>
<p><strong>Examples #3 is highly recommended</strong> for daily usage.</p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>Please see the file BUGS in the CPAN distribution Math::BigInt for known bugs.</p>
<p>
</p>
<hr />
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -