~ubuntu-branches/ubuntu/intrepid/perl-doc-html/intrepid

« back to all changes in this revision

Viewing changes to bigint.html

  • Committer: Bazaar Package Importer
  • Author(s): Roberto C. Sanchez
  • Date: 2008-05-17 20:14:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080517201419-qgbuogq2ckkdisyi
Tags: 5.10.0-2
Supersede botched upload of 5.10.0-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
      <h2>Links:</h2>
55
55
      <ul>
56
56
        <li><a href="http://search.cpan.org">CPAN</a></li>
 
57
        <li><a href="http://www.perl.org">Perl.org</a></li>
57
58
        <li><a href="http://www.perl.com">Perl.com</a></li>
58
 
        <li><a href="http://www.perl.org">Perl.org</a></li>
 
59
        <li><a href="http://perlbuzz.com">Perl Buzz</a></li>
 
60
        <li><a href="http://www.perlfoundation.org/perl5/index.cgi">Perl 5 Wiki</a></li>
 
61
        <li><a href="http://jobs.perl.org">Perl Jobs</a></li>
59
62
        <li><a href="http://www.pm.org">Perl Mongers</a></li>
60
63
        <li><a href="http://www.perlmonks.org">Perl Monks</a></li>
61
64
        <li><a href="http://planet.perl.org">Planet Perl</a></li>
65
68
      <ul>
66
69
        <li>Site maintained by<br><a href="http://perl.jonallen.info">Jon Allen</a>
67
70
            (<a href="http://perl.jonallen.info">JJ</a>)</li>
68
 
        <li class="spaced">Last updated on<br>23 April 2006</li>
 
71
        <li class="spaced">Last updated on<br>23 December 2007</li>
69
72
        <li class="spaced">See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for
70
73
        more details</li>
71
74
      </ul>
76
79
    <div id="centerContent">
77
80
      <div id="contentHeader">
78
81
        <div id="contentHeaderLeft"><a href="#" onClick="showLeft()">Show navigation</a></div>
79
 
        <div id="contentHeaderCentre">-- Perl 5.8.8 documentation --</div>
 
82
        <div id="contentHeaderCentre">-- Perl 5.10.0 documentation --</div>
80
83
        <div id="contentHeaderRight"><a href="#" onClick="showRight()">Show toolbar</a></div>
81
84
      </div>
82
85
      <div id="breadCrumbs"><a href="index.html">Home</a> &gt; <a href="index-pragmas.html">Pragmas</a> &gt; bigint</div>
83
86
      <script language="JavaScript">fromSearch();</script>
84
 
      <div id="contentBody"><div class="title_container"><div class="page_title">bigint</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Options">Options</a><li><a href="#Math-Library">Math Library</a><li><a href="#Internal-Format">Internal Format</a><li><a href="#Sign">Sign</a><li><a href="#Methods">Methods</a><li><a href="#Caveat">Caveat</a></ul><li><a href="#MODULES-USED">MODULES USED</a><li><a href="#EXAMPLES">EXAMPLES</a><li><a href="#LICENSE">LICENSE</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHORS">AUTHORS</a></ul><a name="NAME"></a><h1>NAME</h1>
 
87
      <div id="contentBody"><div class="title_container"><div class="page_title">bigint</div></div><ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#use-integer-vs.-use-bigint">use integer vs. use bigint</a><li><a href="#Options">Options</a><li><a href="#Math-Library">Math Library</a><li><a href="#Internal-Format">Internal Format</a><li><a href="#Sign">Sign</a><li><a href="#Methods">Methods</a><li><a href="#MATH-LIBRARY">MATH LIBRARY</a><li><a href="#Caveat">Caveat</a></ul><li><a href="#CAVAETS">CAVAETS</a><li><a href="#MODULES-USED">MODULES USED</a><li><a href="#EXAMPLES">EXAMPLES</a><li><a href="#LICENSE">LICENSE</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHORS">AUTHORS</a></ul><a name="NAME"></a><h1>NAME</h1>
85
88
<p>bigint - Transparent BigInteger support for Perl</p>
86
89
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
87
 
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="bigint.html">bigint</a><span class="sc">;</span></pre>
 
90
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span><span class="sc">;</span></pre>
88
91
<pre class="verbatim">  <span class="i">$x</span> = <span class="n">2</span> + <span class="n">4.5</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>                        <span class="c"># BigInt 6</span>
89
92
  <a class="l_k" href="functions/print.html">print</a> <span class="n">2</span> ** <span class="n">512</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>                    <span class="c"># really is what you think it is</span>
90
93
  <a class="l_k" href="functions/print.html">print</a> <span class="i">inf</span> + <span class="n">42</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>                    <span class="c"># inf</span>
91
 
  <a class="l_k" href="functions/print.html">print</a> <span class="i">NaN</span> <span class="i">* 7</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>                     <span class="c"># NaN</span></pre>
 
94
  <a class="l_k" href="functions/print.html">print</a> <span class="i">NaN</span> <span class="i">* 7</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>                     <span class="c"># NaN</span>
 
95
  <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/hex.html">hex</a><span class="s">(</span><span class="q">&quot;0x1234567890123490&quot;</span><span class="s">)</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>     <span class="c"># Perl v5.9.4 or later</span></pre>
 
96
<pre class="verbatim">  <span class="s">{</span>
 
97
    <a class="l_k" href="functions/no.html">no</a> <span class="w">bigint</span><span class="sc">;</span>
 
98
    <a class="l_k" href="functions/print.html">print</a> <span class="n">2</span> ** <span class="n">256</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>          <span class="c"># a normal Perl scalar now</span>
 
99
  <span class="s">}</span></pre>
 
100
<pre class="verbatim">  <span class="c"># Note that this will be global:</span>
 
101
  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span> <span class="q">qw/hex oct/</span><span class="sc">;</span>
 
102
  <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/hex.html">hex</a><span class="s">(</span><span class="q">&quot;0x1234567890123490&quot;</span><span class="s">)</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>
 
103
  <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/oct.html">oct</a><span class="s">(</span><span class="q">&quot;01234567890123490&quot;</span><span class="s">)</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span></pre>
92
104
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
93
105
<p>All operators (including basic math operations) are overloaded. Integer
94
106
constants are created as proper BigInts.</p>
95
 
<p>Floating point constants are truncated to integer. All results are also
96
 
truncated.</p>
 
107
<p>Floating point constants are truncated to integer. All parts and results of
 
108
expressions are also truncated.</p>
 
109
<p>Unlike <a href="integer.html">integer</a>, this pragma creates integer constants that are only
 
110
limited in their size by the available memory and CPU time.</p>
 
111
<a name="use-integer-vs.-use-bigint"></a><h2>use integer vs. use bigint</h2>
 
112
<p>There is one small difference between <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span></code>
 
113
 and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span></code>
 
114
: the
 
115
former will not affect assignments to variables and the return value of
 
116
some functions. <code class="inline"><span class="w">bigint</span></code>
 
117
 truncates these results to integer too:</p>
 
118
<pre class="verbatim">  # perl -Minteger -wle 'print 3.2'
 
119
        3.2
 
120
        # perl -Minteger -wle 'print 3.2 + 0'
 
121
        3
 
122
        # perl -Mbigint -wle 'print 3.2'
 
123
        3
 
124
        # perl -Mbigint -wle 'print 3.2 + 0'
 
125
        3</pre><pre class="verbatim">   # perl -Mbigint -wle 'print exp(1) + 0'
 
126
        2
 
127
        # perl -Mbigint -wle 'print exp(1)'
 
128
        2
 
129
        # perl -Minteger -wle 'print exp(1)'
 
130
        2.71828182845905
 
131
        # perl -Minteger -wle 'print exp(1) + 0'
 
132
        2</pre><p>In practice this makes seldom a difference as <b>parts and results</b> of
 
133
expressions will be truncated anyway, but this can, for instance, affect the
 
134
return value of subroutines:</p>
 
135
<pre class="verbatim"><a name="three_integer"></a>      sub <span class="m">three_integer</span> <span class="s">{</span> <a class="l_k" href="functions/use.html">use</a> <span class="w">integer</span><span class="sc">;</span> <a class="l_k" href="functions/return.html">return</a> <span class="n">3.2</span><span class="sc">;</span> <span class="s">}</span> 
 
136
<a name="three_bigint"></a>     sub <span class="m">three_bigint</span> <span class="s">{</span> <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span><span class="sc">;</span> <a class="l_k" href="functions/return.html">return</a> <span class="n">3.2</span><span class="sc">;</span> <span class="s">}</span></pre>
 
137
<pre class="verbatim">  <a class="l_k" href="functions/print.html">print</a> <span class="i">three_integer</span><span class="s">(</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">three_bigint</span><span class="s">(</span><span class="s">)</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>     <span class="c"># prints &quot;3.2 3&quot;</span></pre>
97
138
<a name="Options"></a><h2>Options</h2>
98
139
<p>bigint recognizes some options that can be passed while loading it via use.
99
140
The options can (currently) be either a single letter form, or the long form.
102
143
<li><a name="a-or-accuracy"></a><b>a or accuracy</b>
103
144
<p>This sets the accuracy for all math operations. The argument must be greater
104
145
than or equal to zero. See Math::BigInt's bround() function for details.</p>
105
 
<pre class="verbatim">  perl -Mbigint=a<span class="cm">,</span><span class="n">2</span> -le <span class="q">&#39;print 12345+1&#39;</span></pre>
 
146
<pre class="verbatim">  <span class="w">perl</span> -<span class="w">Mbigint</span>=<span class="w">a</span><span class="cm">,</span><span class="n">2</span> -le <span class="q">&#39;print 12345+1&#39;</span></pre>
 
147
<p>Note that setting precision and accurary at the same time is not possible.</p>
106
148
</li>
107
149
<li><a name="p-or-precision"></a><b>p or precision</b>
108
150
<p>This sets the precision for all math operations. The argument can be any
111
153
A positive value rounds to this digit left from the dot. 0 or 1 mean round to
112
154
integer and are ignore like negative values.</p>
113
155
<p>See Math::BigInt's bfround() function for details.</p>
114
 
<pre class="verbatim">  perl -Mbignum=p<span class="cm">,</span><span class="n">5</span> -le <span class="q">&#39;print 123456789+123&#39;</span></pre>
 
156
<pre class="verbatim">  <span class="w">perl</span> -<span class="w">Mbignum</span>=<span class="w">p</span><span class="cm">,</span><span class="n">5</span> -le <span class="q">&#39;print 123456789+123&#39;</span></pre>
 
157
<p>Note that setting precision and accurary at the same time is not possible.</p>
115
158
</li>
116
159
<li><a name="t-or-trace"></a><b>t or trace</b>
117
160
<p>This enables a trace mode and is primarily for debugging bigint or
118
161
Math::BigInt.</p>
119
162
</li>
120
 
<li><a name="l-or-lib"></a><b>l or lib</b>
121
 
<p>Load a different math lib, see <a href="#MATH-LIBRARY">"MATH LIBRARY"</a>.</p>
122
 
<pre class="verbatim">  perl -Mbigint=l<span class="cm">,</span>GMP -e <span class="q">&#39;print 2 ** 512&#39;</span></pre>
123
 
<p>Currently there is no way to specify more than one library on the command
124
 
line. This will be hopefully fixed soon ;)</p>
 
163
<li><a name="hex"></a><b>hex</b>
 
164
<p>Override the built-in hex() method with a version that can handle big
 
165
integers. Note that under Perl v5.9.4 or ealier, this will be global
 
166
and cannot be disabled with "no bigint;".</p>
 
167
</li>
 
168
<li><a name="oct"></a><b>oct</b>
 
169
<p>Override the built-in oct() method with a version that can handle big
 
170
integers. Note that under Perl v5.9.4 or ealier, this will be global
 
171
and cannot be disabled with "no bigint;".</p>
 
172
</li>
 
173
<li><a name="l%2c-lib%2c-try-or-only"></a><b>l, lib, try or only</b>
 
174
<p>Load a different math lib, see <a href="#Math-Library">"Math Library"</a>.</p>
 
175
<pre class="verbatim">  perl -Mbigint=lib,GMP -e 'print 2 ** 512'
 
176
        perl -Mbigint=try,GMP -e 'print 2 ** 512'
 
177
        perl -Mbigint=only,GMP -e 'print 2 ** 512'</pre><p>Currently there is no way to specify more than one library on the command
 
178
line. This means the following does not work:</p>
 
179
<pre class="verbatim">  <span class="w">perl</span> -<span class="w">Mbignum</span>=<span class="w">l</span><span class="cm">,</span><span class="w">GMP</span><span class="cm">,</span><span class="w">Pari</span> -e <span class="q">'print 2 ** 512'</span></pre>
 
180
<p>This will be hopefully fixed soon ;)</p>
125
181
</li>
126
182
<li><a name="v-or-version"></a><b>v or version</b>
127
183
<p>This prints out the name and version of all modules used and then exits.</p>
128
 
<pre class="verbatim">  perl -Mbigint=v</pre>
 
184
<pre class="verbatim">  <span class="w">perl</span> -<span class="w">Mbigint</span>=<span class="w">v</span></pre>
129
185
</li>
130
186
</ul>
131
187
<a name="Math-Library"></a><h2>Math Library</h2>
132
188
<p>Math with the numbers is done (by default) by a module called
133
189
Math::BigInt::Calc. This is equivalent to saying:</p>
134
 
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="bigint.html">bigint</a> <a class="l_w" href="lib.html">lib</a> <span class="cm">=&gt;</span> <span class="q">&#39;Calc&#39;</span><span class="sc">;</span></pre>
 
190
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span> <span class="w">lib</span> <span class="cm">=&gt;</span> <span class="q">&#39;Calc&#39;</span><span class="sc">;</span></pre>
135
191
<p>You can change this by using:</p>
136
 
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="bigint.html">bigint</a> <a class="l_w" href="lib.html">lib</a> <span class="cm">=&gt;</span> <span class="q">&#39;BitVect&#39;</span><span class="sc">;</span></pre>
 
192
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bignum</span> <span class="w">lib</span> <span class="cm">=&gt;</span> <span class="q">'GMP'</span><span class="sc">;</span></pre>
137
193
<p>The following would first try to find Math::BigInt::Foo, then
138
194
Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:</p>
139
 
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <a class="l_w" href="bigint.html">bigint</a> <a class="l_w" href="lib.html">lib</a> <span class="cm">=&gt;</span> <span class="q">&#39;Foo,Math::BigInt::Bar&#39;</span><span class="sc">;</span></pre>
 
195
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span> <span class="w">lib</span> <span class="cm">=&gt;</span> <span class="q">&#39;Foo,Math::BigInt::Bar&#39;</span><span class="sc">;</span></pre>
 
196
<p>Using <code class="inline"><span class="w">lib</span></code>
 
197
 warns if none of the specified libraries can be found and
 
198
<a href="Math/BigInt.html">Math::BigInt</a> did fall back to one of the default libraries.
 
199
To supress this warning, use <code class="inline"><span class="w">try</span></code>
 
200
 instead:</p>
 
201
<pre class="verbatim">        <a class="l_k" href="functions/use.html">use</a> <span class="w">bignum</span> <span class="w">try</span> <span class="cm">=&gt;</span> <span class="q">'GMP'</span><span class="sc">;</span></pre>
 
202
<p>If you want the code to die instead of falling back, use <code class="inline"><span class="w">only</span></code>
 
203
 instead:</p>
 
204
<pre class="verbatim">        <a class="l_k" href="functions/use.html">use</a> <span class="w">bignum</span> <span class="w">only</span> <span class="cm">=&gt;</span> <span class="q">'GMP'</span><span class="sc">;</span></pre>
140
205
<p>Please see respective module documentation for further details.</p>
141
206
<a name="Internal-Format"></a><h2>Internal Format</h2>
142
207
<p>The numbers are stored as objects, and their internals might change at anytime,
157
222
<a name="Methods"></a><h2>Methods</h2>
158
223
<p>Since all numbers are now objects, you can use all functions that are part of
159
224
the BigInt API. You can only use the bxxx() notation, and not the fxxx()
160
 
notation, though. </p>
 
225
notation, though.</p>
 
226
<ul>
 
227
<li><a name="inf()"></a><b>inf()</b>
 
228
<p>A shortcut to return Math::BigInt-&gt;binf(). Useful because Perl does not always
 
229
handle bareword <code class="inline"><span class="w">inf</span></code>
 
230
 properly.</p>
 
231
</li>
 
232
<li><a name="NaN()"></a><b>NaN()</b>
 
233
<p>A shortcut to return Math::BigInt-&gt;bnan(). Useful because Perl does not always
 
234
handle bareword <code class="inline"><span class="w">NaN</span></code>
 
235
 properly.</p>
 
236
</li>
 
237
<li><a name="e"></a><b>e</b>
 
238
<pre class="verbatim">  <span class="c"># perl -Mbigint=e -wle 'print e'</span></pre>
 
239
<p>Returns Euler's number <code class="inline"><span class="w">e</span></code>
 
240
, aka exp(1). Note that under bigint, this is
 
241
truncated to an integer, and hence simple '2'.</p>
 
242
</li>
 
243
<li><a name="PI"></a><b>PI</b>
 
244
<pre class="verbatim">  <span class="c"># perl -Mbigint=PI -wle 'print PI'</span></pre>
 
245
<p>Returns PI. Note that under bigint, this is truncated to an integer, and hence
 
246
simple '3'.</p>
 
247
</li>
 
248
<li><a name="bexp()"></a><b>bexp()</b>
 
249
<pre class="verbatim">  <span class="i">bexp</span><span class="s">(</span><span class="i">$power</span><span class="cm">,</span><span class="i">$accuracy</span><span class="s">)</span><span class="sc">;</span></pre>
 
250
<p>Returns Euler's number <code class="inline"><span class="w">e</span></code>
 
251
 raised to the appropriate power, to
 
252
the wanted accuracy.</p>
 
253
<p>Note that under bigint, the result is truncated to an integer.</p>
 
254
<p>Example:</p>
 
255
<pre class="verbatim">  <span class="c"># perl -Mbigint=bexp -wle 'print bexp(1,80)'</span></pre>
 
256
</li>
 
257
<li><a name="bpi()"></a><b>bpi()</b>
 
258
<pre class="verbatim">  <span class="i">bpi</span><span class="s">(</span><span class="i">$accuracy</span><span class="s">)</span><span class="sc">;</span></pre>
 
259
<p>Returns PI to the wanted accuracy. Note that under bigint, this is truncated
 
260
to an integer, and hence simple '3'.</p>
 
261
<p>Example:</p>
 
262
<pre class="verbatim">  <span class="c"># perl -Mbigint=bpi -wle 'print bpi(80)'</span></pre>
 
263
</li>
 
264
<li><a name="upgrade()"></a><b>upgrade()</b>
 
265
<p>Return the class that numbers are upgraded to, is in fact returning
 
266
<code class="inline"><span class="i">$Math::BigInt::upgrade</span></code>
 
267
.</p>
 
268
</li>
 
269
<li><a name="in_effect()"></a><b>in_effect()</b>
 
270
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span><span class="sc">;</span></pre>
 
271
<pre class="verbatim">  <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;in effect\n&quot;</span> if <span class="w">bigint::in_effect</span><span class="sc">;</span>        <span class="c"># true</span>
 
272
        <span class="s">{</span>
 
273
          <a class="l_k" href="functions/no.html">no</a> <span class="w">bigint</span><span class="sc">;</span>
 
274
          <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;in effect\n&quot;</span> if <span class="w">bigint::in_effect</span><span class="sc">;</span>      <span class="c"># false</span>
 
275
        <span class="s">}</span></pre>
 
276
<p>Returns true or false if <code class="inline"><span class="w">bigint</span></code>
 
277
 is in effect in the current scope.</p>
 
278
<p>This method only works on Perl v5.9.4 or later.</p>
 
279
</li>
 
280
</ul>
 
281
<a name="MATH-LIBRARY"></a><h2>MATH LIBRARY</h2>
 
282
<p>Math with the numbers is done (by default) by a module called</p>
161
283
<a name="Caveat"></a><h2>Caveat</h2>
162
284
<p>But a warning is in order. When using the following to make a copy of a number,
163
285
only a shallow copy will be made.</p>
168
290
<pre class="verbatim">  <span class="i">$x</span> = <span class="n">9</span><span class="sc">;</span> <span class="i">$y</span> = <span class="i">$x</span><span class="sc">;</span>
169
291
        <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span> + <span class="n">1</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>       <span class="c"># prints 10 9</span></pre>
170
292
<p>but calling any method that modifies the number directly will result in
171
 
<b>both</b> the original and the copy beeing destroyed:
172
 
        
173
 
        $x = 9; $y = $x;
174
 
        print $x-&gt;badd(1), " ", $y,"\n";     # prints 10 10
175
 
        
176
 
        $x = 9; $y = $x;
177
 
        print $x-&gt;binc(1), " ", $y,"\n";     # prints 10 10
178
 
        
179
 
        $x = 9; $y = $x;
180
 
        print $x-&gt;bmul(2), " ", $y,"\n";     # prints 18 18
181
 
        
182
 
Using methods that do not modify, but testthe contents works:</p>
 
293
<b>both</b> the original and the copy being destroyed:</p>
 
294
<pre class="verbatim">  <span class="i">$x</span> = <span class="n">9</span><span class="sc">;</span> <span class="i">$y</span> = <span class="i">$x</span><span class="sc">;</span>
 
295
        <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="i">-&gt;badd</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>  <span class="c"># prints 10 10</span></pre>
 
296
<pre class="verbatim">        <span class="i">$x</span> = <span class="n">9</span><span class="sc">;</span> <span class="i">$y</span> = <span class="i">$x</span><span class="sc">;</span>
 
297
        <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="i">-&gt;binc</span><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>  <span class="c"># prints 10 10</span></pre>
 
298
<pre class="verbatim">  <span class="i">$x</span> = <span class="n">9</span><span class="sc">;</span> <span class="i">$y</span> = <span class="i">$x</span><span class="sc">;</span>
 
299
        <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="i">-&gt;bmul</span><span class="s">(</span><span class="n">2</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot; &quot;</span><span class="cm">,</span> <span class="i">$y</span><span class="cm">,</span><span class="q">&quot;\n&quot;</span><span class="sc">;</span>  <span class="c"># prints 18 18</span></pre>
 
300
<p>Using methods that do not modify, but testthe contents works:</p>
183
301
<pre class="verbatim">  <span class="i">$x</span> = <span class="n">9</span><span class="sc">;</span> <span class="i">$y</span> = <span class="i">$x</span><span class="sc">;</span>
184
302
        <span class="i">$z</span> = <span class="n">9</span> if <span class="i">$x</span><span class="i">-&gt;is_zero</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>           <span class="c"># works fine</span></pre>
185
303
<p>See the documentation about the copy constructor and <code class="inline">=</code>
186
304
 in overload, as
187
305
well as the documentation in BigInt for further details.</p>
 
306
<a name="CAVAETS"></a><h1>CAVAETS</h1>
 
307
<ul>
 
308
<li><a name="in_effect()"></a><b>in_effect()</b>
 
309
<p>This method only works on Perl v5.9.4 or later.</p>
 
310
</li>
 
311
<li><a name="hex()%2foct()"></a><b>hex()/oct()</b>
 
312
<p><code class="inline"><span class="w">bigint</span></code>
 
313
 overrides these routines with versions that can also handle
 
314
big integer values. Under Perl prior to version v5.9.4, however, this
 
315
will not happen unless you specifically ask for it with the two
 
316
import tags "hex" and "oct" - and then it will be global and cannot be
 
317
disabled inside a scope with "no bigint":</p>
 
318
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span> <span class="q">qw/hex oct/</span><span class="sc">;</span></pre>
 
319
<pre class="verbatim">  <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/hex.html">hex</a><span class="s">(</span><span class="q">&quot;0x1234567890123456&quot;</span><span class="s">)</span><span class="sc">;</span>
 
320
        <span class="s">{</span>
 
321
                <a class="l_k" href="functions/no.html">no</a> <span class="w">bigint</span><span class="sc">;</span>
 
322
                <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/hex.html">hex</a><span class="s">(</span><span class="q">&quot;0x1234567890123456&quot;</span><span class="s">)</span><span class="sc">;</span>
 
323
        <span class="s">}</span></pre>
 
324
<p>The second call to hex() will warn about a non-portable constant.</p>
 
325
<p>Compare this to:</p>
 
326
<pre class="verbatim">  <a class="l_k" href="functions/use.html">use</a> <span class="w">bigint</span><span class="sc">;</span></pre>
 
327
<pre class="verbatim">  <span class="c"># will warn only under Perl older than v5.9.4</span>
 
328
        <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/hex.html">hex</a><span class="s">(</span><span class="q">&quot;0x1234567890123456&quot;</span><span class="s">)</span><span class="sc">;</span></pre>
 
329
</li>
 
330
</ul>
188
331
<a name="MODULES-USED"></a><h1>MODULES USED</h1>
189
 
<p><code class="inline"><a class="l_w" href="bigint.html">bigint</a></code> is just a thin wrapper around various modules of the Math::BigInt
 
332
<p><code class="inline"><span class="w">bigint</span></code>
 
333
 is just a thin wrapper around various modules of the Math::BigInt
190
334
family. Think of it as the head of the family, who runs the shop, and orders
191
335
the others to do the work.</p>
192
336
<p>The following modules are currently used by bigint:</p>
193
337
<pre class="verbatim">  Math::BigInt::Lite      (for speed, and only if it is loadable)
194
338
        Math::BigInt</pre><a name="EXAMPLES"></a><h1>EXAMPLES</h1>
195
339
<p>Some cool command line examples to impress the Python crowd ;) You might want
196
 
to compare them to the results under -Mbignum or -Mbigrat:
197
 
 
198
 
        perl -Mbigint -le 'print sqrt(33)'
 
340
to compare them to the results under -Mbignum or -Mbigrat:</p>
 
341
<pre class="verbatim">  perl -Mbigint -le 'print sqrt(33)'
199
342
        perl -Mbigint -le 'print 2*255'
200
343
        perl -Mbigint -le 'print 4.5+2*255'
201
344
        perl -Mbigint -le 'print 3/7 + 5/7 + 8/3'
202
345
        perl -Mbigint -le 'print 123-&gt;is_odd()'
203
346
        perl -Mbigint -le 'print log(2)'
204
347
        perl -Mbigint -le 'print 2 ** 0.5'
205
 
        perl -Mbigint=a,65 -le 'print 2 ** 0.2'</p>
206
 
<a name="LICENSE"></a><h1>LICENSE</h1>
 
348
        perl -Mbigint=a,65 -le 'print 2 ** 0.2'
 
349
        perl -Mbignum=a,65,l,GMP -le 'print 7 ** 7777'</pre><a name="LICENSE"></a><h1>LICENSE</h1>
207
350
<p>This program is free software; you may redistribute it and/or modify it under
208
351
the same terms as Perl itself.</p>
209
352
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
210
 
<p>Especially <a href="bigrat.html">bigrat</a> as in <code class="inline">perl -Mbigrat -le <span class="q">&#39;print 1/3+1/4&#39;</span></code>
 
353
<p>Especially <a href="bigrat.html">bigrat</a> as in <code class="inline"><span class="w">perl</span> -<span class="w">Mbigrat</span> -le <span class="q">&#39;print 1/3+1/4&#39;</span></code>
211
354
 and
212
 
<a href="bignum.html">bignum</a> as in <code class="inline">perl -Mbignum -le <span class="q">&#39;print sqrt(2)&#39;</span></code>
 
355
<a href="bignum.html">bignum</a> as in <code class="inline"><span class="w">perl</span> -<span class="w">Mbignum</span> -le <span class="q">&#39;print sqrt(2)&#39;</span></code>
213
356
.</p>
214
357
<p><a href="Math/BigInt.html">Math::BigInt</a>, <a href="Math/BigRat.html">Math::BigRat</a> and <a href="http://search.cpan.org/perldoc/Math::Big">Math::Big</a> as well
215
358
as <a href="http://search.cpan.org/perldoc/Math::BigInt::BitVect">Math::BigInt::BitVect</a>, <a href="http://search.cpan.org/perldoc/Math::BigInt::Pari">Math::BigInt::Pari</a> and  <a href="http://search.cpan.org/perldoc/Math::BigInt::GMP">Math::BigInt::GMP</a>.</p>
216
359
<a name="AUTHORS"></a><h1>AUTHORS</h1>
217
 
<p>(C) by Tels <a href="http://bloodgate.com/">http://bloodgate.com/</a> in early 2002 - 2005.</p>
 
360
<p>(C) by Tels <a href="http://bloodgate.com/">http://bloodgate.com/</a> in early 2002 - 2007.</p>
218
361
</div>
219
362
      <div id="contentFooter"><a href="http://www.perl.org"><img src="perlpowered.png" border=0></a></div>
220
363
    </div>
232
375
          <!--<select name="r"><option value="1" selected>Go to top result<option value="0">Show results list</select>-->
233
376
        </form>
234
377
      </p>
 
378
      <script language="JavaScript" type="text/javascript" src="/perl-version.js"></script>
235
379
      <h2>Labels:</h2>
236
380
      <p>
237
381
        <a href="#" onClick="addLabel('bigint','bigint.html')">Add this page</a>