Routines for very large numbers.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Routines for very large numbers.

Post by dunbarx » Tue Sep 01, 2015 5:30 pm

Richard.

Of course. I only placed it in its own thread for increased visibility.

Now that MaxV has chimed in, perhaps we can put this whole 64-bit limitation to rest. Finally.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Routines for very large numbers.

Post by FourthWorld » Tue Sep 01, 2015 5:35 pm

dunbarx wrote:Richard.

Of course. I only placed it in its own thread for increased visibility.
Thanks - threads merged.

The taxonomy of the forums is a bit unwieldy, so I can appreciate the desire to move this thread to another location. Which section do you think might be more useful for others who might look for such things?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

hpsh
Posts: 52
Joined: Tue Aug 25, 2015 8:06 pm

Re: Routines for very large numbers.

Post by hpsh » Tue Sep 01, 2015 7:07 pm

has to say thanks for the code :-) and i am really sorry for being complete useless here :-(

was abel to make a code that could multiply some big numbers, but of some reason, it fail at 1010101*1010101.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Routines for very large numbers.

Post by dunbarx » Tue Sep 01, 2015 10:31 pm

Hi.

LC can do "99999999" * "99999999". That is two large eight digit numbers. But as soon as you go into the ninth digit of even one of those, the native limit kicks in, and you get errors in the lowest digits in the product.

So your routine with two seven-digit numbers has something awry.

Craig

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Routines for very large numbers.

Post by golive » Wed Sep 02, 2015 3:03 am

@MaxV

Thanks for your routines. They work well.

Presumably the native behavior will be faster. So I guess one would want to test the numbers to see when the native behavior will be OK to use.

How do you envisage using these in practice?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Routines for very large numbers.

Post by dunbarx » Wed Sep 02, 2015 3:24 am

Both MaxV's routines and mine work by breaking long numeric strings into shorter ones and then reassembling with just a few very small calculations. It isn't a matter of crunching large numbers. In either case, any process of any reasonable length would be nearly instantaneous.

The beneift of being able to use integers of any length may crop up here and there. As I posted earlier, within native limits, you cannot locate yourself in the observable universe closer than a few million miles. This now can be refined as desired; one only needs 40 or so digits to be able to locate oneself in that universe to within the diameter of a proton.

Craig
Last edited by dunbarx on Wed Sep 02, 2015 1:42 pm, edited 1 time in total.

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Routines for very large numbers.

Post by MaxV » Wed Sep 02, 2015 9:15 am

golive wrote:@MaxV
How do you envisage using these in practice?
I'm an engineer, and form year 2000 I encounters problems with pc calculations.
The problem it's huge, but it's invisible to normal men.
For example a 16 bit processor limit is: 65'536. So you start to have some troubles with numbers bigger of 65'536. Many math libraries use the native processor features to be quick, but it's a mess with big numbers. You have to check always what is the biggest number you'll get in your calculations and to choose always the correct library, test it and test it, again and again.
Most of people approximate, without realizing it; but sometimes happens that those calculation are completely wrong. :lol:
It happens most with formulas containing exponentials and square roots, when numbers become extremely big or smallers.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: longAdd and longMult

Post by golive » Thu Sep 03, 2015 1:17 am

dunbarx wrote:
Both work for me.

Craig
um... doesnt work for me. LC7.06 commercial.
3-09-2015 10-11-52 AM.png
Attachments
BigNumber.zip
(2.23 KiB) Downloaded 281 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Routines for very large numbers.

Post by dunbarx » Thu Sep 03, 2015 4:04 am

I tried the routines again, to make sure there were no typos. They work. Are you sure about typos on your end? I cannot imagine that v 6.7, where I am, is different from v. 7. Please check, though I am sure the problem is trivial. But if not, the problem is profound, and must be pounced upon.

Craig

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: longAdd and longMult

Post by MaxV » Thu Sep 03, 2015 9:14 am

golive wrote:
dunbarx wrote:
Both work for me.

Craig
um... doesnt work for me. LC7.06 commercial.
Did you try mine functions? They should work (bigAdd, bigSub, bigMultiply, BigDivide, bigGreater)
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: longAdd and longMult

Post by golive » Fri Sep 04, 2015 1:19 am

MaxV wrote: Did you try mine functions? They should work (bigAdd, bigSub, bigMultiply, BigDivide, bigGreater)
@MaxV
Yes yours work nicely, thanks.

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Routines for very large numbers.

Post by golive » Fri Sep 04, 2015 1:27 am

dunbarx wrote:I tried the routines again, to make sure there were no typos. They work. Are you sure about typos on your end? I cannot imagine that v 6.7, where I am, is different from v. 7. Please check, though I am sure the problem is trivial. But if not, the problem is profound, and must be pounced upon.

Craig
Hi Craig
FYI I supplied my stack above so you should see what I see.

Not a biggie, as we now have MaxV's solution as well.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Routines for very large numbers.

Post by dunbarx » Fri Sep 04, 2015 4:00 am

Hi.

The stack "bigNumber" was corrupted when I tried to open it. Sorry...

Just a thought. The "longMult" handler requires both the "addZero" and "longAdd" handlers as well. You did include those, right?

Craig

golive
Posts: 98
Joined: Wed Jul 01, 2015 5:16 am

Re: Routines for very large numbers.

Post by golive » Sat Sep 05, 2015 5:46 am

dunbarx wrote:Hi.
The stack "bigNumber" was corrupted when I tried to open it. Sorry...
Just a thought. The "longMult" handler requires both the "addZero" and "longAdd" handlers as well. You did include those, right?
Craig
Yes they are included. BTW I downloaded the stack again and its fine here :D

I notice in variables (my picture above) arg1 has a space in it for: put longMult(123456789,10) into tNum
But not for: put longMult(12345678,10) into tNum

Code: Select all

on mouseUp
   put longMult(123456789,10) into tNum
   answer tNum
end mouseUp

    --Requires the "addZero" and "longAdd" functions below
    function longMult arg1,arg2
       put the number of chars of arg1 into index1
       put the number of chars of arg2 into index2
       
       if index1 > 8 then
          repeat with y = index1 down to 1 step -8
             put space after char y of arg1
             if the length of word 1 of arg1 <= 8 then exit repeat
          end repeat
       end if
       
       if index2 > 8 then
          repeat with y = index2 down to 1 step -8
             put space after char y of arg2
             if the length of word 1 of arg2 <= 8 then exit repeat
          end repeat
       end if
       
       put the number of words of arg1 into arg1WordCount
       put the number of words of arg2 into arg2WordCount
       
          repeat with y = 1 to arg1WordCount
           repeat with u = 1 to arg2WordCount
             get ((arg1WordCount - y) * 8) + ((arg2WordCount - u) * 8)
                   put word y of arg1 *  word u of arg2 & addZero((it)) & return after temp
             end repeat
          end repeat
       
          repeat with y = 1 to the number of lines of temp - 1
                put longAdd(line y of temp,line y + 1 of temp) into line y + 1 of temp
          end repeat
       
         return the last line of temp
    end longMult

    function addZero var
       repeat var
          put 0 after zeros
       end repeat
       return zeros
end addZero

istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

Re: Routines for very large numbers.

Post by istech » Sun Sep 06, 2015 5:12 pm

I have been testing the routines over the last couple days and all but one appear to work as expected. The function in question is "BigDivide"

Can someone confirm that if you do.

put bigDivide(99999999,10) into t
answer t

LC stops responding or crashes. This does not happen on every calculation only on some trying to narrow it down.

Post Reply