Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

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.


Thursday, April 7, 2011

Cast vs as operator

I was wondering what's the difference between different types of casting. So I made some tests

var test1:Number;
trace( test1 as String); trace( test1.toString()); trace( String(test1));

//Output:
//null
//NaN
//NaN

OK, so we have two types casting. While 'as' returns us not defined String type (casted value also was not defined), standard casting creates instance that tries to use data casted type

Let's try the opposite way. But this time on defined instance

var test2:String = '100';  
trace( test2 as Number); 
trace( Number(test2));



//Output:
//null
//100

Casting give us instance with data gathered from base class. But 'as' operator one more time returns null. I wondered how does it work. And I found. Check this example:


var test3:Sprite = new Sprite(); 
trace( test3 as DisplayObject); 
trace( test3 as MovieClip);


Operator 'as' is concatenation of checking type of data and casting data. It casts only when base type is descendant of casted type. If expression is inherits casted class then it's returned. Otherwise it returns null