Tcl Library Source Code

Bounty program for improvements to Tcl and certain Tcl packages.

[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


math::bignum - Arbitrary precision integer numbers

Table Of Contents


package require Tcl ?8.4?
package require math::bignum ?3.1?

::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


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:

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.

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.


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]]


Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math :: bignum of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

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.


bignums, math, multiprecision, tcl




Copyright © 2004 Salvatore Sanfilippo
Copyright © 2004 Arjen Markus