I’m developing a computer program and I’m trying to work out some arithmetic algorithms for working with very large numbers. So far I have worked out a plan for addition, subtraction, and multiplication but division seems to be much more complicated. The numbers I’m working with are very large and I cannot process the entire number at once, so I have to perform these math operations piece by piece.
NOTE: For the programmers here, I am using Java which has the BigDecimal class for working with large numbers, but without going into a lengthy explanation I would like to create my own version.
I have looked at the source code for the division in Java’s MutableBigInteger to see if I could get some ideas from it, but it does a lot of things that I don’t know why it’s doing them. I’ve also read up on various division algorithms and they all seem to be about the same level of complexity. But I think once I see the process at work on an actual math problem I can work out the coding details.
I’ve worked out some very simple math problems processing only one digit at a time instead of the entire number.
For addition, this is very simple to do:
6.8 + 8.5 = 8+5=3 (carry 1) 6+8+1=5 (carry 1) 1
And the result is “1 5 3” or 15.3.
Division...not so simple apparently. Here is a basic example which I can’t seem to get to the answer “3” working with only one digit at a time:
7.2 / 2.4 = 3
I’ve tried a few methods such as dividing each digit to get an integer value and using the modulus operation for the remainder to combine with the next digit over, but I’ve only made a mess of things so far. This doesn’t seem like it should be that difficult, but it’s proving to be (I guess that’s why the division algorithms I’ve looked at are so complex)...so I think I need to enlist the help of some math gurus here. Any help on this would be greatly appreciated. Thanks. :)