There is a way to work around it though. The number of numbers between two
integers drops as the number grows, so if you represent your number by
splitting it into more variables, then the precision of your arithmetic will
grow as well.
Try this:
var a:Number = 1.9;
var b:Number = 2.2;
var c = b-a;
trace(c); //0.3
Since it's using lower numbers in the operation, 0.3 is found as the best
representation for the result.
I wrote this to parse number strings to working precisions, I think you
could use a similiar technique to do higher precision arithmetic.
function parsePrecision(a:String){
var b = a.split('.'), l=b[1].length, b=[Number(b[0]), Number(b[1])], c =
b[1]/Math.pow(10, l);
return b[0]>0? b[0]+c:b[0]-c;
}
var s:String = "-952.86";
var i:Number = parsePrecision(s);
trace(i); // -952.86
trace(i - -952.86); // 0
I think I will have at it later today.
Hope this helps,
M.
On 4/28/06, [EMAIL PROTECTED] < [EMAIL PROTECTED]> wrote:
Apologies if this is common knowledge, but I've just come across a huge
maths problem in Flash... as I've mentioned before I'm working on an online
trading system where real people make or lose real money, sometimes a huge
amount of it, so this isn't funny...
Here's some simple arithmetic..
var a:Number = 171.9;
var b:Number = 172.2;
var c:Number;
c = b - a;
Now an elementary school kid would probably give the answer 0.3
Unfortunately Flash has other ideas...
trace(c);
0.299999999999983
I'm a bit shocked to be honest. Am I imagining it?
I'm aware that AS3 introduces proper integers and floats for arithmetic,
but that doesnt address my problem now.
What says the wise Chattyfig community??
_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com