int and Number type madness

Recently I came across a strange behaviour of the AS3 compiler with int and Number types. It took some time to extract the flawed portion of my code to reproduce the error, so here is an example starting with the case where everything’s fine:

var i:int = 3;
var j:int = 6;
while (true)
{
    var k:int = (i + j) / 2;
    trace(k, k is int, k is Number); //4, true, false
    break;
}

The term ‘(i + j) / 2’ evaluates to 4.5, and assigning it to the variable k removes everything after the decimal point. Now below is another example with just the while loop replaced with a do..while variant. But now the variable k “forgets” that it was typed to an int and becomes a Number storing a float:

var i:int = 3;
var j:int = 6;
do
{
    var k:int = (i+ j) / 2;
    trace(k, k is int, k is Number); //4.5, false, true
    break;
}
while (true);

This is really bad since using k to access arrays will throw a runtime error. A workaround is to explicitly cast the value to an int or declare the variable outside the do..while loop.