Computing2011. 4. 22. 22:42


작년 3월경에 데이터 정밀도를 정리하다가 플래시의 Number데이터형에 대하여 뭔가 이상한 점을 발견했었다. 바로 IEEE-754라는 표준을 따른다고 하는데 정작 최대 53bit를 사용하여 정수를 표현한다는 것이다.
IEEE-754 표준의 경우 배정밀도는 64bit를 사용
한다.

오늘 야꼬랑 채팅 중에 과거 이러한 사실이 문득 떠올랐다. 크게 상관있는 일은 아니지만 11bit의 차이는 생각에 따라서 매우 작지만 매우 크기도 한 값이다.

아래는 ActionScript 3.0 레퍼런스의 내용이다. 지금 플래시를 열어서 F1을 누르고 복사한 내용이다.
Language Version:  ActionScript 3.0
Runtime Versions:  AIR 1.0, Flash Player 9, Flash Lite 4
A data type representing an IEEE-754 double-precision floating-point number. You can manipulate primitive numeric values by using the methods and properties associated with the Number class. This class is identical to the JavaScript Number class.
The properties of the Number class are static, which means you do not need an object to use them, so you do not need to use the constructor.

The Number data type adheres to the double-precision IEEE-754 standard.

The Number data type is useful when you need to use floating-point values. Flash runtimes handle int and uint data types more efficiently than Number, but Number is useful in situations where the range of values required exceeds the valid range of the int and uint data types. The Number class can be used to represent integer values well beyond the valid range of the int and uint data types. The Number data type can use up to 53 bits to represent integer values, compared to the 32 bits available to int and uint. The default value of a variable typed as Number is NaN (Not a Number).

위의 볼딕한 내용은 IEEE-754 배정밀도 표준을 사용한다는 것과 최대 53bit를 사용하여 수를 만든다는 것 이다.


아래는 위키백과의 IEEE-754 표준에 대한 설명이다.
IEEE 754컴퓨터에서 부동소수점를 표현하는 가장 널리 쓰이는 표준이다. ±0 등의 수와 무한, NaN 등의 기호를 표시하는 법과 이러한 수에 대한 연산을 정의하고 있다.

IEEE 754에는 32 비트 단정도(single-precision), 64 비트 배정도(double-precision), 43 비트 이상의 확장단정도(거의 쓰이지 않음), 79 비트 이상의 확장배정도(일반적으로 80비트로 구현됨)에 대한 형식을 정의하고 있다. 이중 32 비트 단정도는 반드시 구현해야 하며, 다른 형식은 선택사항이다. 많은 프로그래밍 언어에서 IEEE 표준을 따르도록 정의하고 있다. 예를 들어 C에서는 float는 단정도, double은 배정도와 대응된다.


왜 11비트의 차이가 있는것일까? IEEE-754의 표준도 수를 만들기 위해서 64비트 중에 11비트를 다른 것에 사용하고 53비트만 사용하는 것일까?

흔히 타언어에서 double형은 64비트 즉 8바이트 배정밀도이며 float은 32비트 즉 4바이트 단정밀도이다.
참고로 예전에 테스트한 Decimal산출식 테스트 : 링크열기


그럼 Number 넌 정체가 머냐?
8바이트 보다 작은...6바이트 보다 좀 큰.... 53비트인거냐? 이것은 불충분한 도움말의 오해인것이다. 실수형의 용량만 표기하고 그외의 용량을 표기하지 않았다. 아래의 리플처럼 1비트는 역시 음수와 양수의 구분에 사용하고 실수 52비트까지 포함하여 53비트라고 표시한 것 같다. 나머지 11비트는 지수를 표현하는 것이다.



야꼬의 리플로 다시 찾아본 결과 위키 영문에서야 정확한 포맷을 찾을 수 있었다.

Basic formats

The standard defines five basic formats, named using their base and the number of bits used to encode them. A conforming implementation must fully implement at least one of the basic formats. There are three binary floating-point basic formats (which can be encoded using 32, 64 or 128 bits) and two decimal floating-point basic formats (which can be encoded using 64 or 128 bits). The binary32 and binary64 formats are the single and double formats of IEEE 754-1985.

The precision of the binary formats is one greater than the width of its significand, because there is an implied (hidden) 1 bit.

Name Common name Base Digits E min E max Notes Decimal
digits
Decimal
E max
binary16 Half precision 2 10+1 −14 +15 storage, not basic 3.31 4.51
binary32 Single precision 2 23+1 −126 +127 7.22 38.23
binary64 Double precision 2 52+1 −1022 +1023 15.95 307.95
binary128 Quadruple precision 2 112+1 −16382 +16383 34.02 4931.77
decimal32 10 7 −95 +96 storage, not basic 7 96
decimal64 10 16 −383 +384 16 384
decimal128 10 34 −6143 +6144 34 6144

Decimal digits is digits × log10 base, this gives the precision in decimal.

Decimal E max is Emax × log10 base, this gives the maximum exponent in decimal.

All the basic formats are available in both hardware and software implementations.


결국 같은 정밀도를 갖는다는 것이다!!! 그런데 역시나 의문이 남는다. 예전의 정밀도 테스트한 결과 차이가 분명하였기 때문이다. C에서 double과 AS3에서의 Number형의 테스트 결과 Number의 정밀도가 떨어졌기 때문이다. 소수점 몇 자리까지인지 기억나질 않지만...이런건 언어의 특성이라고 봐야하는 것일까?

Posted by 버터백통