Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Big Numbers

Author
Message
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 13th May 2015 12:52 Edited at: 13th May 2015 14:49
I had a requirement to add very large numbers together in a recent project (we're talking thousands of digits long!) - obviously the AGK2 integer would not suffice, so I wrote a little function to add large numbers stored as strings [EDIT] Got rid of the arrays of INT completely and used some good old string manipulation instead:



example usage:



I would appreciate any hints on optimisation, and I'd love to know if anyone has a comprehensive suite of very large number functions available (addition, subtraction, multiplication and divide) before I go create them from scratch!

V2 T1 (Mostly)
Uzmadesign
Effishen
10
Years of Service
User Offline
Joined: 13th Jun 2014
Location:
Posted: 13th May 2015 17:17
u can skip the bunch where you compare their lengths.

a nested loop with proper exits will work like a charm

Effishen
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 13th May 2015 17:28
Strings are convenient for such things, but there's may well a be fair amount of string thrashing (allocation/copying) overhead as the numbers get bigger. No big deal, if all you need is a few big number addition calls, but may be crippling if you need to add a lot of big values. If that's the case then you;d be better off going down the binary road.

16bit adder.. repeat to add any size of stream you need.



CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 13th May 2015 18:04 Edited at: 13th May 2015 18:05
Thanks for your help guys.

@Effishen I'm not sure what you mean. I need to find the longest of the two number to know how many iterations are required in the for-loop. I can't see how a nested loop will help .

@Kevin - Do you have an AppGameKit equivalent for that code, and can you show me how you can use it to add very long numbers? I don't really understand what "repeat to add any size of stream you need" means Will I need to chop up the long number strings and convert the chunks into 16-bit size ints... tbh all those bitwise operators give me a headache.

V2 T1 (Mostly)
Uzmadesign
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 13th May 2015 19:14 Edited at: 13th May 2015 19:16
Here is a cleaner bigadd$ function: [EDIT] - Now includes bigmultiply$ function



V2 T1 (Mostly)
Uzmadesign
Muckypaws
AGK Developer
12
Years of Service
User Offline
Joined: 21st Oct 2011
Location:
Posted: 14th May 2015 01:29
Depending on your coding abilities, if you can read C code, and TBH it's fairly easy to make the transition to AppGameKit Basic. Take a look at OpenSSL's implementation of Big Numbers. The GitHub repository is located here https://github.com/openssl/openssl/tree/e22d2199e2a5cc9b243f45c2b633d1e31fadecd7/crypto/bn

You can find functions for add/multiply/divide/subtract/prime numbers/modulus etc. Especially if you are looking to develop your own cryptographic library written entirely in BASIC.

[href]https://www.facebook.com/WonkyPix
http://www.wonkypix.com
http://www.appstore.com/jsecconsultancyltd[/href]

Login to post a reply

Server time is: 2024-09-28 23:03:05
Your offset time is: 2024-09-28 23:03:05