(Arbitrarily) Long-Division Script

Igor de Oliveira Couto igor at semperuna.com
Thu Oct 30 20:51:13 EDT 2014

Hi all,

I wanted to develop a library to allow me to perform basic maths (add, subtract, multiply, divide) with arbitrarily long numbers in LiveCode. My requirements are simple: 

- integers and floating-point numbers must be supported as all operands in all operations, to an arbitrarily large number of decimal cases

- speed is NOT an issue: performances can safely be relatively slow, as it is unlikely that it’ll need to perform hundreds of thousands of operations per second

- accuracy IS an issue: needless to say, all operations must provide *accurate* and *reliable* results, regardless of how many decimal cases are used

It proved relatively easy to do the addition, multiplication and subtraction operations in LiveCode. I followed the ‘pen-and-paper’ algorithm, and it all seems to be working really well - I’m happy to provide anybody with a copy of the scripts off-list, if you wish (just send me an email directly). I’m stuck, however, trying to implement DIVISION. There does not seem to be an “easy” pen-and-paper algorithm that would support arbitrarily large numbers with unknown number of decimal cases. 

Most long-division algorithm seems to expect that the number being divided can be of an arbitrarily length, but they expect that the divisor (the number we are dividing BY) is going to be low enough, so that the person making the division will “know” instinctively how many times it would “fit” into the number being divided. These algorithms are not recommended once we start dealing with divisor of 3 digits or more. There does not seem to be an algorithm that would allow us to procede “digit-by-digit” with the division, as can be done with addition/subtraction/multiplication… Or is there?

Searching Google and Wikipedia has yielded articles about using bitwise operations, or complex mathematical theory, both of which are beyond me. Is there a Math Wiz in this list, who could give us a layman’s explantion of an algorithm that could be used? Any help would be much appreciated!

Kindest regards to all,

Igor Couto
Sydney, Australia

More information about the use-livecode mailing list