math::bignum(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

✨ math::bignum ✨

okay bestie, buckle up, because this is the math::bignum page from the Tcl Math Library and it's about to make you feel SO powerful πŸ’ͺ it's all about arbitrary precision integer numbers β€” yes, the big ones, the ones that don't fit anywhere, the main characters πŸ’…


🌸 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 πŸ’–

so the bignum package hands the Tcl language arbitrary precision integer math β€” aka "big numbers", aka numbers that are just built different πŸ”₯ Big numbers are internally represented as Tcl lists, and this package gives you a whole little set of procedures that operate on 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 here's the cute part πŸŽ€ 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, we love a convenient queen πŸ‘‘

the bignum interface is opaque, which means operations on bignums that are not returned by procedures in this package (but created by hand) may lead to unspecified behaviours 😭 so like… don't do that. 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. free of charge, no cleanup era required ✨


πŸ¦‹ 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]]

πŸ‘‘ API πŸ‘‘

  • ::math::bignum::fromstr string ?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::tostr bignum ?radix? β€” Convert bignum into a string representing the number in the specified radix. If radix is omitted, the default is 10.
  • ::math::bignum::sign bignum β€” Return the sign of the bignum. The procedure returns 0 if the number is positive, 1 if it's negative.
  • ::math::bignum::abs bignum β€” Return the absolute value of the bignum. glow up edition ✨
  • ::math::bignum::cmp a b β€” Compare the two bignums a and b, returning 0 if a == b, 1 if a > b, and -1 if a < b.
  • ::math::bignum::iszero bignum β€” Return true if bignum value is zero, otherwise false is returned.
  • ::math::bignum::lt a b β€” Return true if a < b, otherwise false is returned.
  • ::math::bignum::le a b β€” Return true if a <= b, otherwise false is returned.
  • ::math::bignum::gt a b β€” Return true if a > b, otherwise false is returned.
  • ::math::bignum::ge a b β€” Return true if a >= b, otherwise false is returned.
  • ::math::bignum::eq a b β€” Return true if a == b, otherwise false is returned.
  • ::math::bignum::ne a b β€” Return true if a != b, otherwise false is returned.
  • ::math::bignum::isodd bignum β€” Return true if bignum is odd.
  • ::math::bignum::iseven bignum β€” Return true if bignum is even.
  • ::math::bignum::add a b β€” Return the sum of the two bignums a and b.
  • ::math::bignum::sub a b β€” Return the difference of the two bignums a and b.
  • ::math::bignum::mul a b β€” 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. (yes it said "algorith", we're keeping it, iconic typo energy πŸ’€)
  • ::math::bignum::divqr a b β€” 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. two for one, a bundle 🫢
  • ::math::bignum::div a b β€” Return the quotient of the division between the two bignums a and b.
  • ::math::bignum::rem a b β€” Return the remainder of the division between the two bignums a and b.
  • ::math::bignum::mod n m β€” Return n modulo m. This operation is called modular reduction.
  • ::math::bignum::pow base exp β€” Return base raised to the exponent exp.
  • ::math::bignum::powm base exp m β€” Return base raised to the exponent exp, modulo m. This function is often used in the field of cryptography. πŸ”
  • ::math::bignum::sqrt bignum β€” Return the integer part of the square root of bignum
  • ::math::bignum::rand bits β€” Return a random number of at most bits bits. 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. 🚨 so like, cute for a vibe, NOT for your secrets, ok?
  • ::math::bignum::lshift bignum bits β€” 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::rshift bignum bits β€” 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::bitand a 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. 😭
  • ::math::bignum::bitor a 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. 😭
  • ::math::bignum::bitxor a 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. 😭
  • ::math::bignum::setbit bignumVar bit β€” Set the bit at bit position to 1 in the bignum stored in the variable bignumVar. Bit 0 is the least significant.
  • ::math::bignum::clearbit bignumVar bit β€” Set the bit at bit position to 0 in the bignum stored in the variable bignumVar. Bit 0 is the least significant.
  • ::math::bignum::testbit bignum bit β€” 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::bits bignum β€” 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 Tcllib Trackers [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 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 (c) 2004 Salvatore Sanfilippo <antirez at invece dot org>

Copyright (c) 2004 Arjen Markus <arjenmarkus at users dot sourceforge dot net>


and that's the whole page bestie, you ATE that πŸ’… this was tcllib version 3.1.2, math::bignum(n), and you understood all of it. no cap. go forth and multiply (literally, it's ::math::bignum::mul 😭) βœ¨πŸ¦‹πŸ’–

πŸ“¦ where this page comes from