01-11-2011 05:59 PM
Hello, in my application I am multiplying extremely large numbers. The product of these numbers when converted to a String and displayed to the user are in one of these formats:
201.16800 * 0.00000000000668458134 = 1.34472385900512e-9
999999999999999999 * 0.0254 * 0.00000000000668458134 = -0.0002524105470876466
Either like #1 the product is in scientific notation, which is what I want, or like #2 it has a negative value. Why does #2 have a negative value? Also I am using the Number type to store these numbers. Is there a better type more suited for larger numbers - since I know a regular 32-bit integer wouldn't be able to handle these numbers. There must be some solution to this problem. At some point the number would have too many digits to properly display to the user that is why I want to display it in scientific notation like in example #1.
Thanks in advance,
Solved! Go to Solution.
01-11-2011 06:10 PM
Check these links:
01-11-2011 06:10 PM - edited 01-11-2011 06:13 PM
The Number class in Actionscript uses 64 bit double-precision format that allows to store numbers values between -9,007,199,254,740,992 (-2^53) to 9,007,199,254,740,992 (2^53). Your first number in #2 is simply to large to store in the Number class, so you get a positive overflow which results in a negative endresult.
Unfortunately there is no datatype in ActionScript with a larger domain that I know of.
If all this sounds like gibberish to you, I suggest you read up on floating points. The use of such large and small numbers together is boobytrapped by various calculation errors.
01-11-2011 07:15 PM
As others have pointed out, floating point numbers have many (at first glance) mysterious things about them.
Perhaps it would help if you can describe the nature of the calculations you are doing, like the general problem domain. If it won't reveal anything you'd like to keep secret, that is. Possibly there is an alternate way of dealing with the problems you are encountering, which people with greater experience in "numerical methods" may be able to apply to your particular case.
01-11-2011 07:24 PM - edited 01-11-2011 07:35 PM
Well... Thank you for the information that will come in handy, but apparently my error was actually a small error in one of my formulas. The error was actually putting everything into the negative after the first incorrect calculation.
01-11-2011 07:34 PM
One typical approach to doing something like this is to use arbitrary-precision numbers, which are not a standard part of the language.
There is at least one package that I've just fond by searching for "actionscript3 arbitrary precision number", called Granite Data Services BigDecimal. It's an LGPL package, so should be acceptable even if you don't want to make your source code available (unlike if it were GPL). (That link isn't the top level link for the package, but I'm sure you can work your way up from it to where the download is.)
Performance for such packages is generally worse than floating point, but for a typical unit conversion tool that should be completely irrelevant so this ought to work for you.
01-11-2011 08:25 PM
@PBDev, I just noticed he edited his reply just above mine, indicating that he had noticed the error in his calculations.
That said, if the numbers really hadn't fit, at least that library I found would have proven useful.