1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
[comment {-*- tcl -*- doctools manpage}]
[manpage_begin math::bignum n 3.1]
[keywords bignums]
[keywords math]
[keywords multiprecision]
[keywords tcl]
[copyright {2004 Salvatore Sanfilippo <antirez at invece dot org>}]
[copyright {2004 Arjen Markus <arjenmarkus at users dot sourceforge dot net>}]
[moddesc {Tcl Math Library}]
[titledesc {Arbitrary precision integer numbers}]
[category Mathematics]
[require Tcl [opt 8.4]]
[require math::bignum [opt 3.1]]
[description]
[para]
The bignum package provides arbitrary precision integer math
(also known as "big numbers") capabilities to the Tcl language.
Big numbers are internally represented at Tcl lists: this
package provides a set of procedures operating against
the internal representation in order to:
[list_begin itemized]
[item]
perform math operations
[item]
convert bignums from the internal representation to a string in
the desired radix and vice versa.
[list_end]
But the two constants "0" and "1" are automatically converted to
the internal representation, in order to easily compare a number to zero,
or increment a big number.
[para]
The bignum interface is opaque, so
operations on bignums that are not returned by procedures
in this package (but created by hand) may lead to unspecified behaviours.
It's safe to treat bignums as pure values, so there is no need
to free a bignum, or to duplicate it via a special operation.
[section "EXAMPLES"]
This section shows some simple example. This library being just
a way to perform math operations, examples may be the simplest way
to learn how to work with it. Consult the API section of
this man page for information about individual procedures.
[para]
[example_begin]
package require math::bignum
# Multiplication of two bignums
set a [lb]::math::bignum::fromstr 88888881111111[rb]
set b [lb]::math::bignum::fromstr 22222220000000[rb]
set c [lb]::math::bignum::mul $a $b[rb]
puts [lb]::math::bignum::tostr $c[rb] ; # => will output 1975308271604953086420000000
set c [lb]::math::bignum::sqrt $c[rb]
puts [lb]::math::bignum::tostr $c[rb] ; # => will output 44444440277777
# From/To string conversion in different radix
set a [lb]::math::bignum::fromstr 1100010101010111001001111010111 2[rb]
puts [lb]::math::bignum::tostr $a 16[rb] ; # => will output 62ab93d7
# Factorial example
proc fact n {
# fromstr is not needed for 0 and 1
set z 1
for {set i 2} {$i <= $n} {incr i} {
set z [lb]::math::bignum::mul $z [lb]::math::bignum::fromstr $i[rb][rb]
}
return $z
}
puts [lb]::math::bignum::tostr [lb]fact 100[rb][rb]
[example_end]
[section "API"]
[list_begin definitions]
[call [cmd ::math::bignum::fromstr] [arg string] ?[arg radix]?]
Convert [emph string] into a bignum. If [emph radix] is omitted or zero,
the string is interpreted in hex if prefixed with
[emph 0x], in octal if prefixed with [emph ox],
in binary if it's pefixed with [emph bx], as a number in
radix 10 otherwise. If instead the [emph radix] argument
is specified in the range 2-36, the [emph string] is interpreted
in the given radix. Please note that this conversion is
not needed for two constants : [emph 0] and [emph 1]. (see the example)
[call [cmd ::math::bignum::tostr] [arg bignum] ?[arg radix]?]
Convert [emph bignum] into a string representing the number
in the specified radix. If [emph radix] is omitted, the
default is 10.
[call [cmd ::math::bignum::sign] [arg bignum]]
Return the sign of the bignum.
The procedure returns 0 if the number is positive, 1 if it's negative.
[call [cmd ::math::bignum::abs] [arg bignum]]
Return the absolute value of the bignum.
[call [cmd ::math::bignum::cmp] [arg a] [arg b]]
Compare the two bignums a and b, returning [emph 0] if [emph {a == b}],
[emph 1] if [emph {a > b}], and [emph -1] if [emph {a < b}].
[call [cmd ::math::bignum::iszero] [arg bignum]]
Return true if [emph bignum] value is zero, otherwise false is returned.
[call [cmd ::math::bignum::lt] [arg a] [arg b]]
Return true if [emph {a < b}], otherwise false is returned.
[call [cmd ::math::bignum::le] [arg a] [arg b]]
Return true if [emph {a <= b}], otherwise false is returned.
[call [cmd ::math::bignum::gt] [arg a] [arg b]]
Return true if [emph {a > b}], otherwise false is returned.
[call [cmd ::math::bignum::ge] [arg a] [arg b]]
Return true if [emph {a >= b}], otherwise false is returned.
[call [cmd ::math::bignum::eq] [arg a] [arg b]]
Return true if [emph {a == b}], otherwise false is returned.
[call [cmd ::math::bignum::ne] [arg a] [arg b]]
Return true if [emph {a != b}], otherwise false is returned.
[call [cmd ::math::bignum::isodd] [arg bignum]]
Return true if [emph bignum] is odd.
[call [cmd ::math::bignum::iseven] [arg bignum]]
Return true if [emph bignum] is even.
[call [cmd ::math::bignum::add] [arg a] [arg b]]
Return the sum of the two bignums [emph a] and [emph b].
[call [cmd ::math::bignum::sub] [arg a] [arg b]]
Return the difference of the two bignums [emph a] and [emph b].
[call [cmd ::math::bignum::mul] [arg a] [arg b]]
Return the product of the two bignums [emph a] and [emph b].
The implementation uses Karatsuba multiplication if both
the numbers are bigger than a given threshold, otherwise
the direct algorith is used.
[call [cmd ::math::bignum::divqr] [arg a] [arg b]]
Return a two-elements list containing as first element
the quotient of the division between the two bignums
[emph a] and [emph b], and the remainder of the division as second element.
[call [cmd ::math::bignum::div] [arg a] [arg b]]
Return the quotient of the division between the two
bignums [emph a] and [emph b].
[call [cmd ::math::bignum::rem] [arg a] [arg b]]
Return the remainder of the division between the two
bignums [emph a] and [emph b].
[call [cmd ::math::bignum::mod] [arg n] [arg m]]
Return [emph n] modulo [emph m]. This operation is
called modular reduction.
[call [cmd ::math::bignum::pow] [arg base] [arg exp]]
Return [emph base] raised to the exponent [emph exp].
[call [cmd ::math::bignum::powm] [arg base] [arg exp] [arg m]]
Return [emph base] raised to the exponent [emph exp],
modulo [emph m]. This function is often used in the field
of cryptography.
[call [cmd ::math::bignum::sqrt] [arg bignum]]
Return the integer part of the square root of [emph bignum]
[call [cmd ::math::bignum::rand] [arg bits]]
Return a random number of at most [emph bits] bits.
The returned number is internally generated using Tcl's [emph {expr rand()}]
function and is not suitable where an unguessable and cryptographically
secure random number is needed.
[call [cmd ::math::bignum::lshift] [arg bignum] [arg bits]]
Return the result of left shifting [emph bignum]'s binary
representation of [emph bits] positions on the left.
This is equivalent to multiplying by 2^[emph bits] but much faster.
[call [cmd ::math::bignum::rshift] [arg bignum] [arg bits]]
Return the result of right shifting [emph bignum]'s binary
representation of [emph bits] positions on the right.
This is equivalent to dividing by [emph 2^bits] but much faster.
[call [cmd ::math::bignum::bitand] [arg a] [arg b]]
Return the result of doing a bitwise AND operation on a
and b. The operation is restricted to positive numbers,
including zero. When negative numbers are provided as
arguments the result is undefined.
[call [cmd ::math::bignum::bitor] [arg a] [arg b]]
Return the result of doing a bitwise OR operation on a
and b. The operation is restricted to positive numbers,
including zero. When negative numbers are provided as
arguments the result is undefined.
[call [cmd ::math::bignum::bitxor] [arg a] [arg b]]
Return the result of doing a bitwise XOR operation on a
and b. The operation is restricted to positive numbers,
including zero. When negative numbers are provided as
arguments the result is undefined.
[call [cmd ::math::bignum::setbit] [arg bignumVar] [arg bit]]
Set the bit at [emph bit] position to 1 in the bignum stored
in the variable [emph bignumVar]. Bit 0 is the least significant.
[call [cmd ::math::bignum::clearbit] [arg bignumVar] [arg bit]]
Set the bit at [emph bit] position to 0 in the bignum stored
in the variable [emph bignumVar]. Bit 0 is the least significant.
[call [cmd ::math::bignum::testbit] [arg bignum] [arg bit]]
Return true if the bit at the [emph bit] position of [emph bignum]
is on, otherwise false is returned. If [emph bit] is out of
range, it is considered as set to zero.
[call [cmd ::math::bignum::bits] [arg bignum]]
Return the number of bits needed to represent bignum in radix 2.
[list_end]
[para]
[vset CATEGORY {math :: bignum}]
[include ../doctools2base/include/feedback.inc]
[manpage_end]
|