Long Numbers - Adding
This study aims to add one big number to another. The script will only add two positive numbers. This is actually logical because otherwise, it would have to do subtraction, which is the subject of the next page.
I do not claim the code is well written or optimum: only that it is believed to work.
The maximum array size is 4,294,967,295, which means that some billions of digits in base 10 can be manipulated. Of course, we aren't limited to base 10, and can use much bigger bases and therefore potentially bigger numbers. But the practicality of this depends on the browser, computer, etc.
These are two useful words. Precision relates to the number of digits that are shown and accuracy relates to the degree the number relates to the true value. So 1/3=0.334444 is more precise (more digits) than 1/3=0.333, but less accurate.
This page is likely to be of interest to only a few people who are interested in numbers. Writing the code was somewhat difficult, and took some time.
The secret, if there is one, is that while numbers are limited in the number of digits which can be displayed, strings and arrays aren't so limited. What we need to do is to put our numbers into strings or array and do our sums on them.
This page deals with adding and the code can add very large numbers. I don't know the largest size of numbers which you can add, but it is probably limited by your computer, rather than anything else.
The following demonstrates the code on this page. Enter a number in both boxes and click on the add. You can check the accuracy using normal pencil and paper arithmetic. Or get an idea from window's calculator.
While the example numbers aren't that big, (I didn't make them too big because they become difficult to read on the page) but you can enter numbers of any size.
The first thing you may notice is that the font for the numbers is a bit funny, being "monospace". The reason for this is so the numbers can be formatted in our example. It is nearly impossible to do this with a regular font because the type characters can vary so much from font to font, and also in any practical use, our formulas would return a number and not a formatted example as above.
Another thing you might notice is that the numbers are formatted with comma separators. Of course, the Germans use commas where the English use dots, and the French use spaces where the English use commas.
Also you may notice that if you enter non-numerical characters or more than one decimal, then you get an error message. To deal with these issues properly, it would have been better to control the characters that can be typed into the boxes so the user cannot make errors. However, the main purpose of this page is to deal with long numbers and formatting and error correction are secondary.
Before doing anything like calculation, I first removed any spaces or commas (separators) in the original numbers. I used the following function:
The above function uses regular expressions with "replace" to clean up the numbers. It removes any spaces and commas in the original numbers.
The way I decided to add the numbers was to add zeros to the one with the least decimals and then to add them as regular numbers without any decimals. I had therefore to record the original decimal positions so I could put them back at the end.
To do this I used the following function:
The length of the number in the code above includes the decimal point, but when I come to put the decimal back, the length will be one less.
When we remove the decimal point, we are effectively multiplying the number by however decimal places there were in the number with the number with the most decimal places. Later of course, we will replace the decimal point in the correct position. But for now, we need to add zeros to the number with the least decimal places.
This is the code that adds the zeros:
In this code, the variable x always contains the longer number. Therefore, if y is longer after the above process, we swap the two variables so that x is the longer number and y is the shorter number.
We use the following code to swap the numbers, if x isn't longer than y.
This is the main code, and is what this page is really about. The algorithm is the one used in the junior school (England) or gradeschool (USA). That is, you line the numbers up according to the decimal point and, starting at the right, add the numbers, and if the answer is bigger than ten, add the carry digit to the next number. Unlike the calculators used later in school, this algorithm works for any numbers, however big.
Essentially, that's it! We add the two numbers, however, big, and get our answer in the string, s. All that remains is to write out the number. This is only necessary because the code is a demonstration of adding long numbers, and it seems that the output should be nicely formatted. As a function to work out sums, it doesn't need such niceties. These niceties are, however, useful in other applications. Naturally, adding the decimal points is essential.
In the code below, we add the decimal points as necessary and also comma separators for the non-decimal numbers. For this, we use functions. Lastly, we swap the numbers, if necessary to ensure x is the longest number:
The following function inserts the decimal point:
Having put the decimal points back into the number, we will add commas to separate the thousands. The following function puts the commas in. (With slight modification, it could add dots or spaces to suit the Germans or the French).
The code above is heavily commented. Even so, it might require careful study.
The sum is written using a monospace font, because the width of numbers in regular fonts is extremely variable. So a typerwiter font was used.
The place where the writing is done is written like this:
And the class "mon" is:
Here I used monospace, but there are many typewriter fonts which would suit.
In putting in some spaces, I first added some spaces infront of the smaller number, y, so it was the same length as the longer number x. Secondly, there was the possibility that the answer might be one longer than the number x, so this had to be allowed for.
Aligning y to x is easily achieved as below:
And making the two numbers x and y align with the answer is equally easy:
Finally, this writes out the sum:
The following code checks that the two numbers entered are numbers with at most one decimal point:
The function is used as follows:
Note how we try to explain what the problem with the number is so the user has some idea what to do to correct the error.
The next page deals with subtraction.