I created a library for math with big numbers

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
geoffcanyon
Posts: 53
Joined: Thu Aug 15, 2013 9:25 am

I created a library for math with big numbers

Post by geoffcanyon » Sun Nov 18, 2018 9:06 pm

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: I created a library for math with big numbers

Post by bogs » Sun Nov 18, 2018 9:10 pm

Nice. Now if I only had problems that required more than a few digits to solve :(
Image

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I created a library for math with big numbers

Post by Klaus » Sun Nov 18, 2018 9:16 pm

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: I created a library for math with big numbers

Post by bogs » Sun Nov 18, 2018 10:21 pm

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

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: I created a library for math with big numbers

Post by Klaus » Mon Nov 19, 2018 10:39 am

Been there, done that, too boring... :D

geoffcanyon
Posts: 53
Joined: Thu Aug 15, 2013 9:25 am

Re: I created a library for math with big numbers

Post by geoffcanyon » Tue Nov 20, 2018 2:19 am

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.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: I created a library for math with big numbers

Post by bogs » Tue Nov 20, 2018 3:17 am

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
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: I created a library for math with big numbers

Post by [-hh] » Tue Nov 20, 2018 11:40 am

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
shiftLock happens

Post Reply

Return to “Talking LiveCode”