在几乎所有的编程语言中,float 和 double 都是遵循 IEEE754 规范的,即:
对于单精度 float 规则如下:
工具:http://www.binaryconvert.com/convert_float.html
其中:
符号位(S ign bit):1 bit
指数位(E xponent):8bit
尾数(M antissa):23bit
S 指示正负 0 为正 1 为负
E 值指示将尾数左右位移多少位
M 尾数
例如 0.5 :
由于是正数,S 为 0;
0.5 用二进制表示为 0.1 尾数部分为 1.0 左移 1 位 (1.0 ← 0.1) 所以 E 为 127-1 ,01111110
尾数做左侧的 1 为隐藏位置所以尾数全部为 0
0 | 01111110 | 00000000000000000000000 |
---|---|---|
正数,S 为 0 | E 默认是 127(01111111), 1.0 小数左移 1 ,所以 127-1 | 1.0 最左侧的 1 为隐藏位所以全为 0 |
结果:00111111 00000000 00000000 00000000
对于双精度 double 规则如下:
工具:http://www.binaryconvert.com/convert_double.html
对于数字:9007199254740991
0 | 10000110011 | 1111111111111111111111111111111111111111111111111111 |
---|---|---|
正数,S 为 0 | E 默认是 1023(01111111111),此处 110011 为52(移动 52+1=53位) | 9007199254740991 = Math.pow(2,53)-1 可以将所有尾数占满 |
结果:01000011 00111111 11111111 11111111 11111111 11111111 11111111 11111111
Iphone 的 GPU 精度:
PowerVR SGX543MP3 (iPhone 5):
- Floats
- Low: precision = 8, range = 0 to 0 (Not sure, but I think this means that we cannot expect a lowp to actually be able to represent a value reaching exactly 2 or -2, I don't really know of a great way to test this, nor should we over-concern ourselves with these limitations, just use mediump when this could ever be an issue)
- Medium: precision = 10, range = 15 to 15 (meets spec)
- High: precision = 23, range = 127 to 127 (exceeds spec)
- Ints
- Low: range = 23 to 23 (exceeds spec)
- Medium: range = 23 to 23 (exceeds spec)
- High: range = 23 to 23 (exceeds spec)
A7 & PowerVR G6430 (iPad Air):
- Floats
- Low: precision = 10, range = 15 to 15 (exceeds spec)
- Medium: precision = 10, range = 15 to 15 (meets spec)
- High: precision = 23, range = 127 to 127 (exceeds ES 2.0 spec, meets 3.0 spec)
- Ints
- Low: range = 15 to 14 (exceeds spec)
- Medium: range = 15 to 14 (exceeds ES 2.0 spec, meets ES 3.0 spec)
- High: range = 31 to 30 (exceeds ES 2.0 spec, meets ES 3.0 spec)
A8 & PowerVR GX6450 (iPhone 6 Plus):
- Floats
- Low: precision = 10, range = 15 to 15 (exceeds spec)
- Medium: precision = 10, range = 15 to 15 (meets spec)
- High: precision = 23, range = 127 to 127 (exceeds ES 2.0 spec, meets 3.0 spec)
- Ints
- Low: range = 15 to 14 (exceeds spec)
- Medium: range = 15 to 14 (exceeds ES 2.0 spec, meets ES 3.0 spec)
- High: range = 31 to 30 (exceeds ES 2.0 spec, meets ES 3.0 spec)