[讨论]VB的DOUBLE类型数据的存储格式
楼上说得很对。就是保存成浮点类型的。我已经查到了相关资料,并可以解读DOUBLE类型的内存存储格式了。
浮点数的存储格式在IEEE 754标准中规定的,由IEEE(Institute of Electrical and Electronics Engineers)美国电气及电子工程师学会制定。标准名称叫做:二进制浮点数算术标准。由于涉及的内容较多,我就本例中的格式进行介绍。
二进制浮点数是以符号数值表示法格式储存,将最高效位元指定为符号位元(sign bit);“指数部份”,即次高效的e位元,为浮点数中,经指数偏差(exponent bias)处理过后的指数;“小数部份”,即剩下的f位元,为有效位数(significand)减掉有效位数本身的最高效位元。 如下图所示:
+-+--------+-----------------------+
|S| Exp | Fraction |
+-+--------+-----------------------+
再具体到64位双精度,规定是这个样子的:
S EEEEEEEEEEE FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
0 1 11 12 63
算法是:
If E=2047 and F is nonzero, then V=NaN ("Not a number")
If E=2047 and F is zero and S is 1, then V=-Infinity
If E=2047 and F is zero and S is 0, then V=Infinity
If 0<E<2047 then V=(-1)**S * 2 ** (E-1023) * (1.F) where "1.F" is intended to represent the binary number created by prefixing F with an implicit leading 1 and a binary point.
If E=0 and F is nonzero, then V=(-1)**S * 2 ** (-1022) * (0.F) These are "unnormalized" values.
If E=0 and F is zero and S is 1, then V=-0
If E=0 and F is zero and S is 0, then V=0
在本例中,再看看里头的数据:
0012F854 05 00 00 00 80 08 00 00 00 00 00 00 00 00 F0 3F
05表示DOUBLE类型,那么最后8个字节 00 00 00 00 00 00 F0 3F就是具体的数字了, 十六进制的表示就是3FF0000000000000,转成二进制形式就是:
00111111 11110000 00000000 00000000 00000000 00000000 00000000 00000000
S=0,E=01111111111(十进制:1023) F=后面的全0
符合0<E<2047 的情况
那么结果就是 (-1)**S * 2 ** (E-1023) * (1.F)=(-1)**0 * 2 ** (1023-1023) * (1.0)=1.0,转换成整数就是:1。
更深入的阅读请看:
http://zh.wikipedia.org/wiki/IEEE_754#64.E4.BD.8D.E9.9B.99.E7.B2.BE.E5.BA.A6
http://www.psc.edu/general/software/packages/ieee/ieee.php
http://cuiyingfeng.blog.51cto.com/43841/6669/