Monday, July 2, 2012

Why a/b*c is not equal to a*c/b

For some it may be obvious, but it took me a while to understand a problem. So I thought that multiplication and division are alternating. Maybe for Mathematics but not for computer. Let's have a look at example:



var a:Number = 1400;
var b:Number = 10000;
var c:Number = 100;


trace(a/b*c);
trace(a*c/b);

From mathematical point of view those results should be equal. But here what flash returns:


14.000000000000002
14

Of course simple round, toFixed() or other operation will mask this problem but it's not a good solution.
Type Number is double-precision with 64bits representation. Division with result different then integer is represents by number with 16 digits of precision.


No comments:

Post a Comment