views:

108

answers:

5
var n:Number = 1;
trace("n is Number:" + (n is Number)); //true
trace("n is int:" + (n is int)); //true
trace("n is uint:" + (n is uint)); //true

var m:Number = 1;
trace("m is Number:" + (m is Number)); //true
trace("m is int:" + (m is int)); //true
trace("m is uint:" + (m is uint)); //true

They all true! in actionscript, how to tell whether the type of a number if Number or int or uint?

A: 

well, to test whether a number is an integer, you can try:

var epsilon:Number = 1E-6; var n:Number; if (Math.abs(n-int(n))

if a number is negative then it must be an int and cannot be an uint. However, if the number is negative and < -2 147 483 648 then it must be a Number and not an int or an uint. If it is positive and > 4 294 967 295 then it must be a Number too. If it is positive and between 2 147 483 648 and 4 294 967 295 it may be either an uint or a Number. If it is positive and < 2 147 483 648 it may be an int or an uint, or a Number.

Basically, what I'm trying to say is, there's no way to tell. why do you want to tell? what do you need this for? There probably are other solutions to your problem.

jonathanasdf
+2  A: 

It's easy. I put additional variables to show how it works. Hope this helps.

var n:Number = 1;
var v:Number = 1.1;
var r:Number = -1;

trace(getQualifiedClassName (n));//output shows 'int'
trace(getQualifiedClassName (v));//output shows 'Number'
trace(getQualifiedClassName (r));//output shows 'int'
VideoDnd
What he's asking for would expect all of those to return Number, since that's how the variables are typed.
fenomas
+1  A: 

The confusion here stems from a peculiar subtlety about how AS3 handles types and numbers. The is operator tests class inheritance, but int and uint are not actually classes. (That's why they don't get capitalized - because they have no class definition.) They're more like type associations, which if used properly can gain you certain conveniences and performance improvements. But for inheritance purposes, a Number is a Number is a Number.

What this means in practice is, if you make a variable that is typed as, say, uint then Flash will store that value internally in a 32-bit unsigned format (rather than the 64bit format used for Number). And if you change the value of that variable, it will remain in the same format, so the restrictions on uint will be enforced:

var a:uint = 0;
a--;
trace(a); // 4294967295 - it wrapped around

But it's really the reference to your number that is typed as uint, not the number itself. If you make a new untyped reference, this will be apparent:

var a:uint = 0;
var b:* = a;
b--
trace(b); // -1

So to return to your problem, how should you implement your buffer writer? Due to the inherent subtlety in how Flash treats these types I don't think there's an absolutely correct answer. One approach would be to use uint or int if the data meets the restrictions on those types, and use Number otherwise. This would save memory and it could preserve accuracy. But treating all numbers as Number also strikes me as a defensible approach. I think it depends on what you plan to do with the buffer.

fenomas
Thank you for your response. ".. but int and uint are not actually classes..." this answers my confusion. My original idea is to write a simple mapping in ByteArray writers, and to auto-determine the type of data writer by the type of the input data. That's why I was looking for a way to tell Number, int and uint on the fly.
ty
A: 

Well no problem I used getQualifiedClassName method to sort this out. thanks

ASLearner
A: 

Here what I am trying to do:

num1 = random() * 10 num2 = random() * 10

please type in the answer of the following equation:

num1 / num2 = answer

assume num1 = 5 and num2 = 2 then the result is going to be 2.5

Since I want the answers to be of int type such as 1,2,3,4,5 etc, hence I would like to say

if the answer is not of type int (such as 2.5) then genereate a new random of num1 and num2. hope this clarify the issue thanks.

ASLearner