What you could do is look into fixed point maths.

This is how you do maths when you don't want to (can't) use floating
point numbers.  That way you control the precision, it's used in games
as its generally quicker than using decimals and as it uses integers
there isn't any rounding, but you have to compromise either the size of
the number or the precision of the number. 

 

This is explained in many places better than I could.

I've included a few links for starters.

http://home.comcast.net/~ohommes/MathFP/mathfp_bg.html

 

http://www.essentialmath.com/FixedFloat.pps

 

http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/

 

most of the info out there is in C/C++ but you should be able to pick up
something if it doesn't sound too much like overkill.

Kenneth.

 

 

 

From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On
Behalf Of Ken Johnson
Sent: 17 December 2008 14:16
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Rounding error

Hi Everyone -

Given the following code:
var num1:Number = 0.72;
var num2:Number = 0.198;
var num3:Number;

num3 = num1;
num3 += num2;

I would expect num3 to equal 0.918.

Instead, it equals 0.9179999999999999.

And when I apply a number formatter with a precision of 2, I get 0.91.

I am performing financial calculations, and this is not acceptable.

Am I doing something wrong here?

Thanks for your help!
Ken

 

Disclaimer 
-------------------------------------------------------------------------------------------
This electronic message contains information which may be privileged and 
confidential. The information is intended to be for the use of the 
individual(s) or entity named above. If you are not the intended recipient, be 
aware that any disclosure, copying, distribution or use of the contents of this 
information is prohibited. If you have received this electronic message in 
error, please notify us by telephone on 0131 476 6000 and delete the material 
from your computer. 
Registered in Scotland number: SC 172507. 
Registered office address: Quay House 142 Commercial Street Edinburgh EH6 6LB. 

This email message has been scanned for viruses by Mimecast.
-------------------------------------------------------------------------------------------

Reply via email to