Overcoming rounding errors
Floating point variables are prone to rounding errors. (That is, the Number type in ActionScript) Often these don’t matter. But in some applications, especially those that process financial information, it is important to avoid errors. Especially errors that propagate. I am interested in eliminating rounding errors because of my spreadsheet AIR application. e2spreadsheet. The current beta version doesn’t do anything about these errors, but I want to remedy that in the next version.
For example, if you open up e2spreadsheet, and type in the following formula: =74.725*100 what answer do you expect? 7472.5 right? Well, the answer that you actually get is 74.499999999999
Because the binary representation has a finite length, it cannot accurately represent some decimal values.
So, how do we get around this? Well, my first thought was a number representation called Binary Coded Decimal (BCD). I think Cobol used to use this representation. But that would have involved many program operations to simulate each BCD calculation. Then this blog by Josh Tynjala gave me another idea. (Ok, probably not a new idea – but it was a revelation to me).
What if I represent my numbers using a radix-10 (decimal) mantissa and exponent.
Actually floating point numbers are represented internally in binary mantissa-exponent form. So that…
In other words, the mantissa is shifted left or right depending on the value of the exponent.
Rounding errors occur because finite fractional binary numbers don’t translate well into decimal numbers. So the solution is to simulate this mantissa-exponent form in decimal (radix-10). We would write a class so that value=mantissa*Math.pow(10,exponent); And this class would contain all the methods necessary to do arithmetic operations on this representation. Add, Subtract, Multiply, etc..
So, with that idea in my head, I wrote the following code. (previous blog entry which appears below). Don’t rely on this yet. I wrote and tested it quickly, and I wanted to see what other developers think about the idea. Is this a good way to eliminate rounding errors? Or is there a better way I haven’t considered?
Entry filed under: Adobe AIR.