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

走远了吗. 提交于 2019-12-11 03:32:00

问题


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?


回答1:


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.




回答2:


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'



回答3:


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.



来源:https://stackoverflow.com/questions/2669564/in-actionscript-how-to-tell-whether-the-type-of-a-number-if-number-or-int-or-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!