math::bignum(3tcl)
โจ math::bignum โ the manual, but make it make sense โจ
okay bestie, so this is math::bignum, from the tcllib Tcl Math Library (that's the 3tcl manual section, iykyk). It's the page about doing math with arbitrary precision integer numbers, and honestly? It's giving "tiny package, huge energy". ๐
๐ NAME ๐
math::bignum โ Arbitrary precision integer numbers. That's it, that's the whole vibe. Big numbers, no cap. ๐ฆ
๐ธ SYNOPSIS ๐ธ
package require Tcl ?8.5 9?
package require math::bignum ?3.1.2?
::math::bignum::fromstr string ?radix?
::math::bignum::tostr bignum ?radix?
::math::bignum::sign bignum
::math::bignum::abs bignum
::math::bignum::cmp a b
::math::bignum::iszero bignum
::math::bignum::lt a b
::math::bignum::le a b
::math::bignum::gt a b
::math::bignum::ge a b
::math::bignum::eq a b
::math::bignum::ne a b
::math::bignum::isodd bignum
::math::bignum::iseven bignum
::math::bignum::add a b
::math::bignum::sub a b
::math::bignum::mul a b
::math::bignum::divqr a b
::math::bignum::div a b
::math::bignum::rem a b
::math::bignum::mod n m
::math::bignum::pow base exp
::math::bignum::powm base exp m
::math::bignum::sqrt bignum
::math::bignum::rand bits
::math::bignum::lshift bignum bits
::math::bignum::rshift bignum bits
::math::bignum::bitand a b
::math::bignum::bitor a b
::math::bignum::bitxor a b
::math::bignum::setbit bignumVar bit
::math::bignum::clearbit bignumVar bit
::math::bignum::testbit bignum bit
::math::bignum::bits bignum
๐ซถ DESCRIPTION ๐ซถ
The bignum package hands the Tcl language arbitrary precision integer math โ aka "big numbers" โ capabilities. ๐งฎโจ
So here's the tea: big numbers are internally represented as Tcl lists. This package gives you a whole set of procedures that operate against that internal representation, so you can:
- perform math operations
- convert bignums from the internal representation to a string in the desired radix and vice versa.
But โ plot twist โ the two constants "0" and "1" are automatically converted to the internal representation, so you can easily compare a number to zero, or increment a big number. No extra steps, slay. ๐
The bignum interface is opaque, meaning: operations on bignums that were not returned by procedures in this package (but were created by hand) may lead to unspecified behaviours. ๐
But 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. No memory drama, we love that for us. ๐
๐ 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.
package require math::bignum
# Multiplication of two bignums
set a [::math::bignum::fromstr 88888881111111]
set b [::math::bignum::fromstr 22222220000000]
set c [::math::bignum::mul $a $b]
puts [::math::bignum::tostr $c] ; # => will output 1975308271604953086420000000
set c [::math::bignum::sqrt $c]
puts [::math::bignum::tostr $c] ; # => will output 44444440277777
# From/To string conversion in different radix
set a [::math::bignum::fromstr 1100010101010111001001111010111 2]
puts [::math::bignum::tostr $a 16] ; # => 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 [::math::bignum::mul $z [::math::bignum::fromstr $i]]
}
return $z
}
puts [::math::bignum::tostr [fact 100]]
Likeโฆ fact 100?? That's main character energy right there. ๐ฅ
๐ API ๐
okay here comes the whole roster, bestie. Every single one, no skips. ๐
::math::bignum::fromstr string ?radix?โ Convertstringinto a bignum. Ifradixis omitted or zero, the string is interpreted in hex if prefixed with0x, in octal if prefixed withox, in binary if it's pefixed withbx, as a number in radix 10 otherwise. If instead theradixargument is specified in the range 2-36, the string is interpreted in the given radix. Please note that this conversion is not needed for two constants:0and1. (see the example)::math::bignum::tostr bignum ?radix?โ Convertbignuminto a string representing the number in the specifiedradix. Ifradixis omitted, the default is10.::math::bignum::sign bignumโ Return the sign of thebignum. The procedure returns0if the number is positive,1if it's negative.::math::bignum::abs bignumโ Return the absolute value of thebignum.::math::bignum::cmp a bโ Compare the two bignumsaandb, returning0ifa == b,1ifa > b, and-1ifa < b.::math::bignum::iszero bignumโ Return true ifbignumvalue is zero, otherwise false is returned.::math::bignum::lt a bโ Return true ifa < b, otherwise false is returned.::math::bignum::le a bโ Return true ifa <= b, otherwise false is returned.::math::bignum::gt a bโ Return true ifa > b, otherwise false is returned.::math::bignum::ge a bโ Return true ifa >= b, otherwise false is returned.::math::bignum::eq a bโ Return true ifa == b, otherwise false is returned.::math::bignum::ne a bโ Return true ifa != b, otherwise false is returned.::math::bignum::isodd bignumโ Return true ifbignumis odd.::math::bignum::iseven bignumโ Return true ifbignumis even.::math::bignum::add a bโ Return the sum of the two bignumsaandb.::math::bignum::sub a bโ Return the difference of the two bignumsaandb.::math::bignum::mul a bโ Return the product of the two bignumsaandb. The implementation uses Karatsuba multiplication if both the numbers are bigger than a given threshold, otherwise the direct algorith is used.::math::bignum::divqr a bโ Return a two-elements list containing as first element the quotient of the division between the two bignumsaandb, and the remainder of the division as second element.::math::bignum::div a bโ Return the quotient of the division between the two bignumsaandb.::math::bignum::rem a bโ Return the remainder of the division between the two bignumsaandb.::math::bignum::mod n mโ Returnnmodulom. This operation is called modular reduction.::math::bignum::pow base expโ Returnbaseraised to the exponentexp.::math::bignum::powm base exp mโ Returnbaseraised to the exponentexp, modulom. This function is often used in the field of cryptography. ๐ต๏ธโ๏ธ::math::bignum::sqrt bignumโ Return the integer part of the square root ofbignum.::math::bignum::rand bitsโ Return a random number of at mostbitsbits.
The returned number is internally generated using Tcl's expr rand() function and is not suitable where an unguessable and cryptographically secure random number is needed. Not for secrets, bestie, fr fr. ๐จ
::math::bignum::lshift bignum bitsโ Return the result of left shiftingbignum's binary representation ofbitspositions on the left. This is equivalent to multiplying by2^bitsbut much faster.::math::bignum::rshift bignum bitsโ Return the result of right shiftingbignum's binary representation ofbitspositions on the right. This is equivalent to dividing by2^bitsbut much faster.::math::bignum::bitand a bโ Return the result of doing a bitwise AND operation onaandb.
The operation is restricted to positive numbers, including zero. When negative numbers are provided as arguments the result is undefined. ๐
::math::bignum::bitor a bโ Return the result of doing a bitwise OR operation onaandb.
The operation is restricted to positive numbers, including zero. When negative numbers are provided as arguments the result is undefined. ๐
::math::bignum::bitxor a bโ Return the result of doing a bitwise XOR operation onaandb.
The operation is restricted to positive numbers, including zero. When negative numbers are provided as arguments the result is undefined. ๐
::math::bignum::setbit bignumVar bitโ Set the bit atbitposition to1in the bignum stored in the variablebignumVar. Bit 0 is the least significant.::math::bignum::clearbit bignumVar bitโ Set the bit atbitposition to0in the bignum stored in the variablebignumVar. Bit 0 is the least significant.::math::bignum::testbit bignum bitโ Return true if the bit at thebitposition ofbignumis on, otherwise false is returned. Ifbitis out of range, it is considered as set to zero.::math::bignum::bits bignumโ Return the number of bits needed to representbignumin radix 2.
๐ BUGS, IDEAS, FEEDBACK ๐
This document, and the package it describes, will undoubtedly contain bugs and other problems. ๐ญ (yes, even docs, we're all just doing our best out here)
Please report such in the category math :: bignum of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist].
Please also report any ideas for enhancements you may have for either package and/or documentation. We love a glow-up suggestion. โจ
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.
โญ KEYWORDS โญ
bignums, math, multiprecision, tcl
๐ CATEGORY ๐
Mathematics
๐ COPYRIGHT ๐
Copyright (c) 2004 Salvatore Sanfilippo <antirez at invece dot org>
Copyright (c) 2004 Arjen Markus <arjenmarkus at users dot sourceforge dot net>
okay that's the whole page, you ate it, bestie. ๐ซถ This was math::bignum(3tcl) from tcllib, version 3.1.2 โ periodt. ๐
๐ธ
๐ฆ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 ยท Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 ยท Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 ยท Tcl
- Copyright (c) 2004 Salvatore Sanfilippo <antirez at invece dot org>
- Copyright (c) 2004 Arjen Markus <arjenmarkus at users dot sourceforge dot net>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms