int(blah)给出来自(blah)的不同答案为int

时间:2022-06-19 18:27:11

Newbie ActionScript 3 question: why does

新手ActionScript 3问题:为什么呢

(Math.sqrt((r * r - (r - i) * (r - i)) as Number) * 2) as int

give me a different result from

给我一个不同的结果

int(Math.sqrt((r * r - (r - i) * (r - i)) as Number) * 2)

2 个解决方案

#1


The as operator is a direct cast, whereas int() implicitly finds the floor of the Number (note that it doesn't actually call Math.floor, though). The Adobe docs for as say it checks that the "first operand is a member of the data type specified by the second operand." Since 9.59 is not representable as an int, the as cast fails a returns null, while int() first finds the floor of the number, then casts it to int.

as运算符是直接强制转换,而int()隐式查找Number的底层(注意它实际上并不调用Math.floor)。例如,Adobe文档检查“第一个操作数是第二个操作数指定的数据类型的成员”。由于9.59不能表示为int,因此as cast失败返回null,而int()首先找到数字的底限,然后将其转换为int。

You could do Math.floor(blah) as int, and it should work, though it would be slower. Assuming you want a rounded int, Math.round(blah) as int would be more correct, but int(blah + .5) would be fastest and round correctly.

您可以将Math.floor(blah)作为int,它应该可以工作,但速度会慢一些。假设你想要一个舍入的int,Math.round(blah)作为int将更正确,但int(blah + .5)将是最快和正确的圆。

#2


The as operator is not much as a cast, more something like:

as运算符不像一个演员,更像是:

i is int ? int : null;

我是int? int:null;

This is confusing as hell. It checks if the variable is of that type, if it is, the variable is returned, else you'd get null (0 for an int).

这让人很困惑。它检查变量是否属于该类型,如果是,则返回变量,否则您将获得null(0表示int)。

#1


The as operator is a direct cast, whereas int() implicitly finds the floor of the Number (note that it doesn't actually call Math.floor, though). The Adobe docs for as say it checks that the "first operand is a member of the data type specified by the second operand." Since 9.59 is not representable as an int, the as cast fails a returns null, while int() first finds the floor of the number, then casts it to int.

as运算符是直接强制转换,而int()隐式查找Number的底层(注意它实际上并不调用Math.floor)。例如,Adobe文档检查“第一个操作数是第二个操作数指定的数据类型的成员”。由于9.59不能表示为int,因此as cast失败返回null,而int()首先找到数字的底限,然后将其转换为int。

You could do Math.floor(blah) as int, and it should work, though it would be slower. Assuming you want a rounded int, Math.round(blah) as int would be more correct, but int(blah + .5) would be fastest and round correctly.

您可以将Math.floor(blah)作为int,它应该可以工作,但速度会慢一些。假设你想要一个舍入的int,Math.round(blah)作为int将更正确,但int(blah + .5)将是最快和正确的圆。

#2


The as operator is not much as a cast, more something like:

as运算符不像一个演员,更像是:

i is int ? int : null;

我是int? int:null;

This is confusing as hell. It checks if the variable is of that type, if it is, the variable is returned, else you'd get null (0 for an int).

这让人很困惑。它检查变量是否属于该类型,如果是,则返回变量,否则您将获得null(0表示int)。