Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / man / man3 / bignum.3
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "bignum 3"
132.TH bignum 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134bignum \- Transparent BigNumber support for Perl
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 1
138\& use bignum;
139.Ve
140.PP
141.Vb 4
142\& $x = 2 + 4.5,"\en"; # BigFloat 6.5
143\& print 2 ** 512 * 0.1,"\en"; # really is what you think it is
144\& print inf * inf,"\en"; # prints inf
145\& print NaN * 3,"\en"; # prints NaN
146.Ve
147.SH "DESCRIPTION"
148.IX Header "DESCRIPTION"
149All operators (including basic math operations) are overloaded. Integer and
150floating-point constants are created as proper BigInts or BigFloats,
151respectively.
152.PP
153If you do
154.PP
155.Vb 1
156\& use bignum;
157.Ve
158.PP
159at the top of your script, Math::BigFloat and Math::BigInt will be loaded
160and any constant number will be converted to an object (Math::BigFloat for
161floats like 3.1415 and Math::BigInt for integers like 1234).
162.PP
163So, the following line:
164.PP
165.Vb 1
166\& $x = 1234;
167.Ve
168.PP
169creates actually a Math::BigInt and stores a reference to in \f(CW$x\fR.
170This happens transparently and behind your back, so to speak.
171.PP
172You can see this with the following:
173.PP
174.Vb 1
175\& perl -Mbignum -le 'print ref(1234)'
176.Ve
177.PP
178Don't worry if it says Math::BigInt::Lite, bignum and friends will use Lite
179if it is installed since it is faster for some operations. It will be
180automatically upgraded to BigInt whenever neccessary:
181.PP
182.Vb 1
183\& perl -Mbignum -le 'print ref(2**255)'
184.Ve
185.PP
186This also means it is a bad idea to check for some specific package, since
187the actual contents of \f(CW$x\fR might be something unexpected. Due to the
188transparent way of bignum \f(CW\*(C`ref()\*(C'\fR should not be neccessary, anyway.
189.PP
190Since Math::BigInt and BigFloat also overload the normal math operations,
191the following line will still work:
192.PP
193.Vb 1
194\& perl -Mbignum -le 'print ref(1234+1234)'
195.Ve
196.PP
197Since numbers are actually objects, you can call all the usual methods from
198BigInt/BigFloat on them. This even works to some extent on expressions:
199.PP
200.Vb 4
201\& perl -Mbignum -le '$x = 1234; print $x->bdec()'
202\& perl -Mbignum -le 'print 1234->binc();'
203\& perl -Mbignum -le 'print 1234->binc->badd(6);'
204\& perl -Mbignum -le 'print +(1234)->binc()'
205.Ve
206.PP
207(Note that print doesn't do what you expect if the expression starts with
208\&'(' hence the \f(CW\*(C`+\*(C'\fR)
209.PP
210You can even chain the operations together as usual:
211.PP
212.Vb 2
213\& perl -Mbignum -le 'print 1234->binc->badd(6);'
214\& 1241
215.Ve
216.PP
217Under bignum (or bigint or bigrat), Perl will \*(L"upgrade\*(R" the numbers
218appropriately. This means that:
219.PP
220.Vb 2
221\& perl -Mbignum -le 'print 1234+4.5'
222\& 1238.5
223.Ve
224.PP
225will work correctly. These mixed cases don't do always work when using
226Math::BigInt or Math::BigFloat alone, or at least not in the way normal Perl
227scalars work.
228.PP
229If you do want to work with large integers like under \f(CW\*(C`use integer;\*(C'\fR, try
230\&\f(CW\*(C`use bigint;\*(C'\fR:
231.PP
232.Vb 2
233\& perl -Mbigint -le 'print 1234.5+4.5'
234\& 1238
235.Ve
236.PP
237There is also \f(CW\*(C`use bigrat;\*(C'\fR which gives you big rationals:
238.PP
239.Vb 2
240\& perl -Mbigrat -le 'print 1234+4.1'
241\& 12381/10
242.Ve
243.PP
244The entire upgrading/downgrading is still experimental and might not work
245as you expect or may even have bugs.
246.PP
247You might get errors like this:
248.PP
249.Vb 2
250\& Can't use an undefined value as an ARRAY reference at
251\& /usr/local/lib/perl5/5.8.0/Math/BigInt/Calc.pm line 864
252.Ve
253.PP
254This means somewhere a routine got a BigFloat/Lite but expected a BigInt (or
255vice versa) and the upgrade/downgrad path was missing. This is a bug, please
256report it so that we can fix it.
257.PP
258You might consider using just Math::BigInt or Math::BigFloat, since they
259allow you finer control over what get's done in which module/space. For
260instance, simple loop counters will be Math::BigInts under \f(CW\*(C`use bignum;\*(C'\fR and
261this is slower than keeping them as Perl scalars:
262.PP
263.Vb 1
264\& perl -Mbignum -le 'for ($i = 0; $i < 10; $i++) { print ref($i); }'
265.Ve
266.PP
267Please note the following does not work as expected (prints nothing), since
268overloading of '..' is not yet possible in Perl (as of v5.8.0):
269.PP
270.Vb 1
271\& perl -Mbignum -le 'for (1..2) { print ref($_); }'
272.Ve
273.Sh "Options"
274.IX Subsection "Options"
275bignum recognizes some options that can be passed while loading it via use.
276The options can (currently) be either a single letter form, or the long form.
277The following options exist:
278.IP "a or accuracy" 2
279.IX Item "a or accuracy"
280This sets the accuracy for all math operations. The argument must be greater
281than or equal to zero. See Math::BigInt's \fIbround()\fR function for details.
282.Sp
283.Vb 1
284\& perl -Mbignum=a,50 -le 'print sqrt(20)'
285.Ve
286.IP "p or precision" 2
287.IX Item "p or precision"
288This sets the precision for all math operations. The argument can be any
289integer. Negative values mean a fixed number of digits after the dot, while
290a positive value rounds to this digit left from the dot. 0 or 1 mean round to
291integer. See Math::BigInt's \fIbfround()\fR function for details.
292.Sp
293.Vb 1
294\& perl -Mbignum=p,-50 -le 'print sqrt(20)'
295.Ve
296.IP "t or trace" 2
297.IX Item "t or trace"
298This enables a trace mode and is primarily for debugging bignum or
299Math::BigInt/Math::BigFloat.
300.IP "l or lib" 2
301.IX Item "l or lib"
302Load a different math lib, see \*(L"\s-1MATH\s0 \s-1LIBRARY\s0\*(R".
303.Sp
304.Vb 1
305\& perl -Mbignum=l,GMP -e 'print 2 ** 512'
306.Ve
307.Sp
308Currently there is no way to specify more than one library on the command
309line. This will be hopefully fixed soon ;)
310.IP "v or version" 2
311.IX Item "v or version"
312This prints out the name and version of all modules used and then exits.
313.Sp
314.Vb 1
315\& perl -Mbignum=v
316.Ve
317.Sh "Methods"
318.IX Subsection "Methods"
319Beside \fIimport()\fR and \s-1\fIAUTOLOAD\s0()\fR there are only a few other methods.
320.Sp
321Since all numbers are now objects, you can use all functions that are part of
322the BigInt or BigFloat \s-1API\s0. It is wise to use only the \fIbxxx()\fR notation, and not
323the \fIfxxx()\fR notation, though. This makes it possible that the underlying object
324might morph into a different class than BigFloat.
325.Sh "Caveat"
326.IX Subsection "Caveat"
327But a warning is in order. When using the following to make a copy of a number,
328only a shallow copy will be made.
329.Sp
330.Vb 2
331\& $x = 9; $y = $x;
332\& $x = $y = 7;
333.Ve
334.Sp
335If you want to make a real copy, use the following:
336.Sp
337.Vb 1
338\& $y = $x->copy();
339.Ve
340.Sp
341Using the copy or the original with overloaded math is okay, e.g. the
342following work:
343.Sp
344.Vb 2
345\& $x = 9; $y = $x;
346\& print $x + 1, " ", $y,"\en"; # prints 10 9
347.Ve
348.Sp
349but calling any method that modifies the number directly will result in
350\&\fBboth\fR the original and the copy beeing destroyed:
351.Sp
352.Vb 2
353\& $x = 9; $y = $x;
354\& print $x->badd(1), " ", $y,"\en"; # prints 10 10
355.Ve
356.Sp
357.Vb 2
358\& $x = 9; $y = $x;
359\& print $x->binc(1), " ", $y,"\en"; # prints 10 10
360.Ve
361.Sp
362.Vb 2
363\& $x = 9; $y = $x;
364\& print $x->bmul(2), " ", $y,"\en"; # prints 18 18
365.Ve
366.Sp
367Using methods that do not modify, but testthe contents works:
368.Sp
369.Vb 2
370\& $x = 9; $y = $x;
371\& $z = 9 if $x->is_zero(); # works fine
372.Ve
373.Sp
374See the documentation about the copy constructor and \f(CW\*(C`=\*(C'\fR in overload, as
375well as the documentation in BigInt for further details.
376.RS 2
377.IP "\fIinf()\fR" 2
378.IX Item "inf()"
379A shortcut to return Math::BigInt\->\fIbinf()\fR. Usefull because Perl does not always
380handle bareword \f(CW\*(C`inf\*(C'\fR properly.
381.IP "\fINaN()\fR" 2
382.IX Item "NaN()"
383A shortcut to return Math::BigInt\->\fIbnan()\fR. Usefull because Perl does not always
384handle bareword \f(CW\*(C`NaN\*(C'\fR properly.
385.IP "\fIupgrade()\fR" 2
386.IX Item "upgrade()"
387Return the class that numbers are upgraded to, is in fact returning
388\&\f(CW$Math::BigInt::upgrade\fR.
389.RE
390.RS 2
391.Sh "\s-1MATH\s0 \s-1LIBRARY\s0"
392.IX Subsection "MATH LIBRARY"
393Math with the numbers is done (by default) by a module called
394Math::BigInt::Calc. This is equivalent to saying:
395.Sp
396.Vb 1
397\& use bignum lib => 'Calc';
398.Ve
399.Sp
400You can change this by using:
401.Sp
402.Vb 1
403\& use bignum lib => 'BitVect';
404.Ve
405.Sp
406The following would first try to find Math::BigInt::Foo, then
407Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
408.Sp
409.Vb 1
410\& use bignum lib => 'Foo,Math::BigInt::Bar';
411.Ve
412.Sp
413Please see respective module documentation for further details.
414.Sh "\s-1INTERNAL\s0 \s-1FORMAT\s0"
415.IX Subsection "INTERNAL FORMAT"
416The numbers are stored as objects, and their internals might change at anytime,
417especially between math operations. The objects also might belong to different
418classes, like Math::BigInt, or Math::BigFLoat. Mixing them together, even
419with normal scalars is not extraordinary, but normal and expected.
420.Sp
421You should not depend on the internal format, all accesses must go through
422accessor methods. E.g. looking at \f(CW$x\fR\->{sign} is not a bright idea since there
423is no guaranty that the object in question has such a hashkey, nor is a hash
424underneath at all.
425.Sh "\s-1SIGN\s0"
426.IX Subsection "SIGN"
427The sign is either '+', '\-', 'NaN', '+inf' or '\-inf' and stored seperately.
428You can access it with the \fIsign()\fR method.
429.Sp
430A sign of 'NaN' is used to represent the result when input arguments are not
431numbers or as a result of 0/0. '+inf' and '\-inf' represent plus respectively
432minus infinity. You will get '+inf' when dividing a positive number by 0, and
433\&'\-inf' when dividing any negative number by 0.
434.SH "MODULES USED"
435.IX Header "MODULES USED"
436\&\f(CW\*(C`bignum\*(C'\fR is just a thin wrapper around various modules of the Math::BigInt
437family. Think of it as the head of the family, who runs the shop, and orders
438the others to do the work.
439.Sp
440The following modules are currently used by bignum:
441.Sp
442.Vb 3
443\& Math::BigInt::Lite (for speed, and only if it is loadable)
444\& Math::BigInt
445\& Math::BigFloat
446.Ve
447.SH "EXAMPLES"
448.IX Header "EXAMPLES"
449Some cool command line examples to impress the Python crowd ;)
450.Sp
451.Vb 8
452\& perl -Mbignum -le 'print sqrt(33)'
453\& perl -Mbignum -le 'print 2*255'
454\& perl -Mbignum -le 'print 4.5+2*255'
455\& perl -Mbignum -le 'print 3/7 + 5/7 + 8/3'
456\& perl -Mbignum -le 'print 123->is_odd()'
457\& perl -Mbignum -le 'print log(2)'
458\& perl -Mbignum -le 'print 2 ** 0.5'
459\& perl -Mbignum=a,65 -le 'print 2 ** 0.2'
460.Ve
461.SH "LICENSE"
462.IX Header "LICENSE"
463This program is free software; you may redistribute it and/or modify it under
464the same terms as Perl itself.
465.SH "SEE ALSO"
466.IX Header "SEE ALSO"
467Especially bigrat as in \f(CW\*(C`perl \-Mbigrat \-le 'print 1/3+1/4'\*(C'\fR.
468.Sp
469Math::BigFloat, Math::BigInt, Math::BigRat and Math::Big as well
470as Math::BigInt::BitVect, Math::BigInt::Pari and Math::BigInt::GMP.
471.SH "AUTHORS"
472.IX Header "AUTHORS"
473(C) by Tels <http://bloodgate.com/> in early 2002, 2003.