Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS
Accepted Solution

Number Too Large?

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:

 

#1

201.16800 * 0.00000000000668458134 = 1.34472385900512e-9

 

#2

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,

Noah NU

 

 

From,
Noah NU
Developer
Posts: 1,158
Registered: ‎12-29-2010
My Device: PlayBook, Z10 LE, Dev Alpha C
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Number Too Large?

[ Edited ]

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.

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Number Too Large?

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.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 138
Registered: ‎01-09-2011
My Device: Blackberry Tablet OS

Re: Number Too Large?

[ Edited ]

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.

 

Thanks

Noah NU

From,
Noah NU
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Number Too Large?

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.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Trusted Contributor
Posts: 108
Registered: ‎12-28-2010
My Device: Free PlayBook
My Carrier: Verizon

Re: Number Too Large?

How are you getting that answer for #2? With this:

 

var ans:Number = 999999999999999999 * 0.0254 * 0.00000000000668458134; trace(ans); 

 

 I'm getting 169788.366036

Highlighted
Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Number Too Large?

@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. Smiley Wink


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!