Decimal system math



Computers are awesome! But have you ever tried to use them for scientific math? Have you ever made a computer software that performs a long list of mathematical operations and later returns a result which must be precise?


If you have tried any of the endeavors above then you must be frustrated with the fact that computers are not reliable when you need high precision, or when you need to perform many operations sequentially.


Most programmers have sometimes encountered the common floating point problem, when the result is not what you expected, you spend hours trying to discover where the bug is, only to discover the floating point variables were slowly suffering a distortion in their values over the course of the program, all thanks to the binary number system.


That is because computers work with the binary number system, not the decimal system. A computer can understand number such as 0.5 or 0.25, because they are the result of a division by a denominator which is a power of 2 (0.5 is 1/2 and 0.25 is 1/4). But computers are unable to understand all other decimal values, such as 0.1 or 0.35, your computer is merely able to reach an approximation.


A little curiosity: this flaw in computer hardware is what accidentally led to the discovery of the Chaos Theory. A tiny difference in value, over time, may build up and create a very unexpected result. This is a hardware problem, you can't change that, your CPU is built to work this way.


But I've found a solution for it, instead of using floating point variables, such as "float", "double" and "real", I used strings. I created a software which interprets numbers as text instead of floating point values.


Once the software has all the values stored as strings then it will use algorithms to perform all mathematical operations, the algorithms are the exact same you used when you were in elementary school. It was necessary to write functions which are able to perform all the steps of the math algorithms just like a person would do with pen and paper.


Using the D programming language, I've written a whole module which contains all functions for you to perform all mathematical operations, with the precision of the decimal system, there is no deviation between what you write and what the computer understands, therefore it is much more reliable if you are programming something related to scientific computing.


You may think it is inefficient, but it uses little RAM memory since strings are nothing more than a group of bytes, and it is very fast, faster than I had imagined. Running on my AMD FX-4300 CPU it was able to perform 183k additions per sec, 134k subtractions per sec, 240k multiplications per sec, 31k divisions per sec, 30k powers per sec and 80 roots per sec, all with a precision of 20 digits, very impressive. I know the roots are much slower than the other operations, but 80 roots/sec is still quite fast.


I'm aware there are other ways to do it, such as Binary-Coded Decimal, but my implementation was very elegant because of its simplicity, I'm very proud.


Here is the link to it, that way you can learn more and use it: https://github.com/MuriloMir/Decimal-calculator


Please let me know what you are using it for, and the results you got.

Comments