bignum math library
Geoff Canyon
gcanyon at gmail.com
Sun Nov 18 13:28:30 EST 2018
I've created bignum libraries in the past, but never organized it or did
all four operators. So I started with earlier versions of some of the
functions, optimized, corrected, and extended, and here it is:
https://github.com/gcanyon/bignum
It includes functions for addition, subtraction, multiplication, division,
and comparison/equals. All functions handle any length of argument. Most
have options that handle signed arguments. The functions are optimized for
things like different-length arguments.
Some performance benchmarks on my five-year-old laptop:
Add two 10,000 digit numbers: 0.005 seconds.
Add two 10,000 digit numbers: 0.004 seconds.
Multiply two 10,000 digit numbers: 3.1 seconds.
Divide a 10,000 digit number by a 5,000 digit number: 9.4 seconds -- needs
some optimization.
For division in particular, I wrote a faster algorithm that is included,
but there is a bug in the final steps that I haven't figured out yet.
bigDivFlawed is about 40% faster than bigDiv, but can bork the last few
digits of the quotient. Included in case anyone sees the logic error.
If anyone has any suggestions, let me know here, or file bug reports on
github, or code and submit a pull request.
A list of the functions included:
function bigAdd -- returns the sum
function bigAddSigned -- handles positive and negative arguments
function bigSubtract -- returns the difference
function bigSubtractSigned -- handles positive and negative arguments
function bigTimes -- returns the product
function bigTimesSigned -- handles positive and negative arguments
function bigDiv -- returns (the integer quotient),(the remainder)
function bigDivSigned -- handles positive and negative arguments
function bigishDiv -- Handles any length dividend; divisor must be a valid
LC number (< 16 digits)
function bigDivFlawed -- About 40% faster than bigDiv, but can bork the
last few digits of the quotient. Included in case anyone sees the logic
error.
function bigCompare -- Applies >,<,=,<=,>= to any length arguments
function stripLeadingZeros -- returns any string without whatever leading
zeros it contains
More information about the use-livecode
mailing list