logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

math::bignum - Arbitrary precision integer numbers

Api

::math::bignum::fromstrstring ?radix?
              Convert string into a bignum. If radix is omitted or zero, the string is  interpreted  in  hex  if
              prefixed  with 0x, in octal if prefixed with ox, in binary if it's pefixed with bx, as a number in
              radix 10 otherwise. If instead the radix argument 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 :
              0 and 1. (see the example)

       ::math::bignum::tostrbignum ?radix?
              Convert  bignum into a string representing the number in the specified radix. If radix is omitted,
              the default is 10.

       ::math::bignum::signbignum
              Return the sign of the bignum.  The procedure returns 0 if the  number  is  positive,  1  if  it's
              negative.

       ::math::bignum::absbignum
              Return the absolute value of the bignum.

       ::math::bignum::cmpab
              Compare the two bignums a and b, returning 0 if a==b, 1 if a>b, and -1 if a<b.

       ::math::bignum::iszerobignum
              Return true if bignum value is zero, otherwise false is returned.

       ::math::bignum::ltab
              Return true if a<b, otherwise false is returned.

       ::math::bignum::leab
              Return true if a<=b, otherwise false is returned.

       ::math::bignum::gtab
              Return true if a>b, otherwise false is returned.

       ::math::bignum::geab
              Return true if a>=b, otherwise false is returned.

       ::math::bignum::eqab
              Return true if a==b, otherwise false is returned.

       ::math::bignum::neab
              Return true if a!=b, otherwise false is returned.

       ::math::bignum::isoddbignum
              Return true if bignum is odd.

       ::math::bignum::isevenbignum
              Return true if bignum is even.

       ::math::bignum::addab
              Return the sum of the two bignums a and b.

       ::math::bignum::subab
              Return the difference of the two bignums a and b.

       ::math::bignum::mulab
              Return  the  product of the two bignums a and b.  The implementation uses Karatsuba multiplication
              if both the numbers are bigger than a given threshold, otherwise the direct algorith is used.

       ::math::bignum::divqrab
              Return a two-elements list containing as first element the quotient of the  division  between  the
              two bignums a and b, and the remainder of the division as second element.

       ::math::bignum::divab
              Return the quotient of the division between the two bignums a and b.

       ::math::bignum::remab
              Return the remainder of the division between the two bignums a and b.

       ::math::bignum::modnm
              Return n modulo m. This operation is called modular reduction.

       ::math::bignum::powbaseexp
              Return base raised to the exponent exp.

       ::math::bignum::powmbaseexpm
              Return  base  raised  to  the  exponent exp, modulo m. This function is often used in the field of
              cryptography.

       ::math::bignum::sqrtbignum
              Return the integer part of the square root of bignum::math::bignum::randbits
              Return a random number of at most bits bits.  The returned number is  internally  generated  using
              Tcl's  exprrand() function and is not suitable where an unguessable and cryptographically secure
              random number is needed.

       ::math::bignum::lshiftbignumbits
              Return the result of left shifting bignum's binary representation of bits positions on  the  left.
              This is equivalent to multiplying by 2^bits but much faster.

       ::math::bignum::rshiftbignumbits
              Return the result of right shifting bignum's binary representation of bits positions on the right.
              This is equivalent to dividing by 2^bits but much faster.

       ::math::bignum::bitandab
              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.

       ::math::bignum::bitorab
              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.

       ::math::bignum::bitxorab
              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.

       ::math::bignum::setbitbignumVarbit
              Set  the  bit  at  bit  position to 1 in the bignum stored in the variable bignumVar. Bit 0 is the
              least significant.

       ::math::bignum::clearbitbignumVarbit
              Set the bit at bit position to 0 in the bignum stored in the variable  bignumVar.  Bit  0  is  the
              least significant.

       ::math::bignum::testbitbignumbit
              Return true if the bit at the bit position of bignum is on, otherwise false is returned. If bit is
              out of range, it is considered as set to zero.

       ::math::bignum::bitsbignum
              Return the number of bits needed to represent bignum in radix 2.

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 TcllibTrackers [http://core.tcl.tk/tcllib/reportlist].
       Please also report any ideas for enhancements you may have for either package and/or documentation.

       When proposing code changes, please provide unifieddiffs, 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.

Category

       Mathematics

Description

       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:

       •      perform math operations

       •      convert bignums from the internal representation to a string in the desired radix and vice versa.

       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.

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

Keywords

       bignums, math, multiprecision, tcl

Name

       math::bignum - Arbitrary precision integer numbers

Synopsis

       package require Tcl?8.59?

       package require math::bignum?3.1.2?::math::bignum::fromstrstring ?radix?

       ::math::bignum::tostrbignum ?radix?

       ::math::bignum::signbignum::math::bignum::absbignum::math::bignum::cmpab::math::bignum::iszerobignum::math::bignum::ltab::math::bignum::leab::math::bignum::gtab::math::bignum::geab::math::bignum::eqab::math::bignum::neab::math::bignum::isoddbignum::math::bignum::isevenbignum::math::bignum::addab::math::bignum::subab::math::bignum::mulab::math::bignum::divqrab::math::bignum::divab::math::bignum::remab::math::bignum::modnm::math::bignum::powbaseexp::math::bignum::powmbaseexpm::math::bignum::sqrtbignum::math::bignum::randbits::math::bignum::lshiftbignumbits::math::bignum::rshiftbignumbits::math::bignum::bitandab::math::bignum::bitorab::math::bignum::bitxorab::math::bignum::setbitbignumVarbit::math::bignum::clearbitbignumVarbit::math::bignum::testbitbignumbit::math::bignum::bitsbignum

________________________________________________________________________________________________________________

See Also