Page 1 of 1

I created a library for math with big numbers

Posted: Sun Nov 18, 2018 9:06 pm
by geoffcanyon
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

Re: I created a library for math with big numbers

Posted: Sun Nov 18, 2018 9:10 pm
by bogs
Nice. Now if I only had problems that required more than a few digits to solve :(

Re: I created a library for math with big numbers

Posted: Sun Nov 18, 2018 9:16 pm
by Klaus
bogs wrote:
Sun Nov 18, 2018 9:10 pm
Nice. Now if I only had problems that required more than a few digits to solve :(
Hehe, exactly my thoughts! :D

Re: I created a library for math with big numbers

Posted: Sun Nov 18, 2018 10:21 pm
by bogs
This inspires me, what do you say Klaus, team up to program a neutron collider? That would certainly let us play with his new toys I betcha :twisted:

Re: I created a library for math with big numbers

Posted: Mon Nov 19, 2018 10:39 am
by Klaus
Been there, done that, too boring... :D

Re: I created a library for math with big numbers

Posted: Tue Nov 20, 2018 2:19 am
by geoffcanyon
bogs wrote:
Sun Nov 18, 2018 9:10 pm
Nice. Now if I only had problems that required more than a few digits to solve :(
Yeah, I get that. I first did some bignum code to support solutions to problems on Project Euler. Other than that I've never needed a bignum library. But I've worked with J, and having unlimited precision integer math available there is nice.

Re: I created a library for math with big numbers

Posted: Tue Nov 20, 2018 3:17 am
by bogs
No doubt at all on my part, way back in my way back, there were plenty of times I could have used such a library :? Thankfully, my life has become simpler these days :shock:

Levity in my previous remark aside, I was sincere when I said 'Nice', as it surely is when you have a use for crunching such numbers. Mariasole recently presented such a problem with primes, in fact. Recent enough I remember it anyway :D

Re: I created a library for math with big numbers

Posted: Tue Nov 20, 2018 11:40 am
by [-hh]
Some thoughts on the need for more than a few digits.
https://en.wikipedia.org/wiki/Arbitrary ... arithmetic

And yet another "big-numbers-stack": DecimalJS,
a GUI to the javaScript library "Decimal.js", see
http://forums.livecode.com/viewtopic.ph ... 71#p173471