1math::bignum(n)                Tcl Math Library                math::bignum(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       math::bignum - Arbitrary precision integer numbers
9

SYNOPSIS

11       package require Tcl  ?8.4?
12
13       package require math::bignum  ?3.1?
14
15       ::math::bignum::fromstr string ?radix?
16
17       ::math::bignum::tostr bignum ?radix?
18
19       ::math::bignum::sign bignum
20
21       ::math::bignum::abs bignum
22
23       ::math::bignum::cmp a b
24
25       ::math::bignum::iszero bignum
26
27       ::math::bignum::lt a b
28
29       ::math::bignum::le a b
30
31       ::math::bignum::gt a b
32
33       ::math::bignum::ge a b
34
35       ::math::bignum::eq a b
36
37       ::math::bignum::ne a b
38
39       ::math::bignum::isodd bignum
40
41       ::math::bignum::iseven bignum
42
43       ::math::bignum::add a b
44
45       ::math::bignum::sub a b
46
47       ::math::bignum::mul a b
48
49       ::math::bignum::divqr a b
50
51       ::math::bignum::div a b
52
53       ::math::bignum::rem a b
54
55       ::math::bignum::mod n m
56
57       ::math::bignum::pow base exp
58
59       ::math::bignum::powm base exp m
60
61       ::math::bignum::sqrt bignum
62
63       ::math::bignum::rand bits
64
65       ::math::bignum::lshift bignum bits
66
67       ::math::bignum::rshift bignum bits
68
69       ::math::bignum::bitand a b
70
71       ::math::bignum::bitor a b
72
73       ::math::bignum::bitxor a b
74
75       ::math::bignum::setbit bignumVar bit
76
77       ::math::bignum::clearbit bignumVar bit
78
79       ::math::bignum::testbit bignum bit
80
81       ::math::bignum::bits bignum
82
83_________________________________________________________________
84

DESCRIPTION

86       The  bignum  package  provides  arbitrary  precision integer math (also
87       known as "big numbers") capabilities to the Tcl language.  Big  numbers
88       are internally represented at Tcl lists: this package provides a set of
89       procedures operating against the internal representation in order to:
90
91       ·      perform math operations
92
93       ·      convert bignums from the internal representation to a string  in
94              the desired radix and vice versa.  But the two constants "0" and
95              "1" are automatically converted to the internal  representation,
96              in  order to easily compare a number to zero, or increment a big
97              number.
98
99       The bignum interface is opaque, so operations on bignums that  are  not
100       returned  by  procedures in this package (but created by hand) may lead
101       to unspecified behaviours.  It's safe to treat bignums as pure  values,
102       so  there is no need to free a bignum, or to duplicate it via a special
103       operation.
104

EXAMPLES

106       This section shows some simple example. This library being just  a  way
107       to  perform  math operations, examples may be the simplest way to learn
108       how to work with it. Consult the API  section  of  this  man  page  for
109       information about individual procedures.
110
111           package require math::bignum
112
113           # Multiplication of two bignums
114           set a [::math::bignum::fromstr 88888881111111]
115           set b [::math::bignum::fromstr 22222220000000]
116           set c [::math::bignum::mul $a $b]
117           puts [::math::bignum::tostr $c] ; # => will output 1975308271604953086420000000
118           set c [::math::bignum::sqrt $c]
119           puts [::math::bignum::tostr $c] ; # => will output 44444440277777
120
121           # From/To string conversion in different radix
122           set a [::math::bignum::fromstr 1100010101010111001001111010111 2]
123           puts [::math::bignum::tostr $a 16] ; # => will output 62ab93d7
124
125           # Factorial example
126           proc fact n {
127               # fromstr is not needed for 0 and 1
128               set z 1
129               for {set i 2} {$i <= $n} {incr i} {
130                   set z [::math::bignum::mul $z [::math::bignum::fromstr $i]]
131               }
132               return $z
133           }
134
135           puts [::math::bignum::tostr [fact 100]]
136
137

API

139       ::math::bignum::fromstr string ?radix?
140              Convert  string  into a bignum. If radix is omitted or zero, the
141              string is interpreted in hex if prefixed with 0x,  in  octal  if
142              prefixed with ox, in binary if it's pefixed with bx, as a number
143              in radix 10 otherwise. If instead the radix argument  is  speci‐
144              fied  in  the range 2-36, the string is interpreted in the given
145              radix. Please note that this conversion is not  needed  for  two
146              constants : 0 and 1. (see the example)
147
148       ::math::bignum::tostr bignum ?radix?
149              Convert  bignum  into  a  string  representing the number in the
150              specified radix. If radix is omitted, the default is 10.
151
152       ::math::bignum::sign bignum
153              Return the sign of the bignum.  The procedure returns 0  if  the
154              number is positive, 1 if it's negative.
155
156       ::math::bignum::abs bignum
157              Return the absolute value of the bignum.
158
159       ::math::bignum::cmp a b
160              Compare the two bignums a and b, returning 0 if a == b, 1 if a >
161              b, and -1 if a < b.
162
163       ::math::bignum::iszero bignum
164              Return  true  if  bignum  value  is  zero,  otherwise  false  is
165              returned.
166
167       ::math::bignum::lt a b
168              Return true if a < b, otherwise false is returned.
169
170       ::math::bignum::le a b
171              Return true if a <= b, otherwise false is returned.
172
173       ::math::bignum::gt a b
174              Return true if a > b, otherwise false is returned.
175
176       ::math::bignum::ge a b
177              Return true if a >= b, otherwise false is returned.
178
179       ::math::bignum::eq a b
180              Return true if a == b, otherwise false is returned.
181
182       ::math::bignum::ne a b
183              Return true if a != b, otherwise false is returned.
184
185       ::math::bignum::isodd bignum
186              Return true if bignum is odd.
187
188       ::math::bignum::iseven bignum
189              Return true if bignum is even.
190
191       ::math::bignum::add a b
192              Return the sum of the two bignums a and b.
193
194       ::math::bignum::sub a b
195              Return the difference of the two bignums a and b.
196
197       ::math::bignum::mul a b
198              Return  the product of the two bignums a and b.  The implementa‐
199              tion uses Karatsuba multiplication if both the numbers are  big‐
200              ger  than  a  given  threshold, otherwise the direct algorith is
201              used.
202
203       ::math::bignum::divqr a b
204              Return a two-elements list containing as first element the  quo‐
205              tient  of  the division between the two bignums a and b, and the
206              remainder of the division as second element.
207
208       ::math::bignum::div a b
209              Return the quotient of the division between the  two  bignums  a
210              and b.
211
212       ::math::bignum::rem a b
213              Return  the  remainder of the division between the two bignums a
214              and b.
215
216       ::math::bignum::mod n m
217              Return n modulo m. This operation is called modular reduction.
218
219       ::math::bignum::pow base exp
220              Return base raised to the exponent exp.
221
222       ::math::bignum::powm base exp m
223              Return base raised to the exponent exp, modulo m. This  function
224              is often used in the field of cryptography.
225
226       ::math::bignum::sqrt bignum
227              Return the integer part of the square root of bignum
228
229       ::math::bignum::rand bits
230              Return  a random number of at most bits bits.  The returned num‐
231              ber is internally generated using Tcl's expr rand() function and
232              is  not  suitable  where  an  unguessable  and cryptographically
233              secure random number is needed.
234
235       ::math::bignum::lshift bignum bits
236              Return the result of left shifting bignum's  binary  representa‐
237              tion  of bits positions on the left.  This is equivalent to mul‐
238              tiplying by 2^bits but much faster.
239
240       ::math::bignum::rshift bignum bits
241              Return the result of right shifting bignum's binary  representa‐
242              tion  of  bits  positions  on  the right.  This is equivalent to
243              dividing by 2^bits but much faster.
244
245       ::math::bignum::bitand a b
246              Return the result of doing a bitwise AND operation on a  and  b.
247              The operation is restricted to positive numbers, including zero.
248              When negative numbers are provided as arguments  the  result  is
249              undefined.
250
251       ::math::bignum::bitor a b
252              Return  the  result  of doing a bitwise OR operation on a and b.
253              The operation is restricted to positive numbers, including zero.
254              When  negative  numbers  are provided as arguments the result is
255              undefined.
256
257       ::math::bignum::bitxor a b
258              Return the result of doing a bitwise XOR operation on a  and  b.
259              The operation is restricted to positive numbers, including zero.
260              When negative numbers are provided as arguments  the  result  is
261              undefined.
262
263       ::math::bignum::setbit bignumVar bit
264              Set  the  bit  at  bit position to 1 in the bignum stored in the
265              variable bignumVar. Bit 0 is the least significant.
266
267       ::math::bignum::clearbit bignumVar bit
268              Set the bit at bit position to 0 in the  bignum  stored  in  the
269              variable bignumVar. Bit 0 is the least significant.
270
271       ::math::bignum::testbit bignum bit
272              Return true if the bit at the bit position of bignum is on, oth‐
273              erwise false is returned. If bit is out of range, it is  consid‐
274              ered as set to zero.
275
276       ::math::bignum::bits bignum
277              Return the number of bits needed to represent bignum in radix 2.
278

KEYWORDS

280       bignums, math, multiprecision, tcl
281
283       Copyright (c) 2004 Salvatore Sanfilippo <antirez at invece dot org>
284       Copyright (c) 2004 Arjen Markus <arjenmarkus at users dot sourceforge dot net>
285
286
287
288
289math                                  3.1                      math::bignum(n)
Impressum