在Actionscript 3中,为什么动态类型的大int值是Number? [英] In Actionscript 3, why is the dynamic type of large int values Number?

查看:27
本文介绍了在Actionscript 3中,为什么动态类型的大int值是Number?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到如果我创建一个大值的int,对象的动态类型似乎是Number.

I have noticed that if I create an int with a large value, the dynamic type of the object seems to be Number.

示例:

var int1:int = 0x8000000;
var type1:String = flash.utils.getQualifiedClassName(int1); // Returns "int"

var int2:int = 0x10000000;
var type2:String = flash.utils.getQualifiedClassName(int2); // Returns "Number"

这里发生了什么?这两个值都远低于 int 的最大值,即 2,147,483,647 (2^31-1).

What is going on here? Both of the values are well below the maximum value of an int, which is 2,147,483,647 (2^31-1).

推荐答案

发生的事情是 AS3 虚拟机使用 32 位原子"来存储原始类型的值.

What's going on is that the AS3 virtual machine uses 32 bit "atoms" to store the values of primitive types.

这 32 位中的 3 位用于描述类型,这意味着还剩下 29 位.

3 of those 32 bits are used for describing the type, meaning there are 29 bits left.

因为 int 是有符号类型 - 使用 1 位作为符号 - 剩下 28 位作为正整数.使您可以在剩余的 28 位中写入的最大数字:

Since int is a signed type - using 1 bit for the sign - that leaves 28 bits for a positive integer. Making the highest number you can write in the remaining 28 bits:

0x0FFFFFFF = 268435455

只要整数需要超过 29 位,VM 就会将原子更改为 Number 类型(实际上表示为指向实际 64 位双精度浮点数的 29 位指针)).

As soon as the integer needs more than 29 bits, the atom is changed by the VM into a Number type (which is really represented as a 29 bit pointer to the actual 64 bit double precision float).

因此,定义为int 的最大值"的值并没有多大意义(因为 Number 可以存储最多 53 位的整数值,但是 int 将其限制为 32) - 除了以下方面:

So, the value defined as "maximum value of an int" doesn't really make all that much sense (since Number can store integral values up to 53 bits, but int will limit it to 32) - other than in terms of:

  • 与其他语言/技术交流(大多数使用 32 位作为整数)

  • communicating with other languages/technologies (which mostly use 32 bits for their integers)

使习惯于 32 位整数的程序员可以预测结果(大部分);和

making the results (mostly) predictable to programmers used to 32 bit integers; and

面向未来(以防将来内部表示发生变化).

future-proofing (in case the internal representation changes in the future).

作为旁注,整数和数字有很多特点 - 包括:

As a sidenote, there are quite a few peculiarities with ints and Numbers - including that this:

var i:Number = 2.0;

... 存储为 int 直到实际需要小数.

... is stored as int until the decimals are actually needed.

这篇关于在Actionscript 3中,为什么动态类型的大int值是Number?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆