浮点数除了常见的单精度float和双精度double之外,还有个半精度的浮点数,定义于IEEE 754标准中,占用两个字节,是不是很意外?
浮点数 = 符号位 S * ( 1+ 尾数M ) * 2 ^ ( E - 指数偏移 )
class FFloat16 { public: union { struct { #if PLATFORM_LITTLE_ENDIAN uint16 Mantissa : 10; uint16 Exponent : 5; uint16 Sign : 1; #else uint16 Sign : 1; uint16 Exponent : 5; uint16 Mantissa : 10; #endif } Components; uint16 Encoded; }; } class FFloat32 { public: union { struct { #if PLATFORM_LITTLE_ENDIAN uint32 Mantissa : 23; uint32 Exponent : 8; uint32 Sign : 1; #else uint32 Sign : 1; uint32 Exponent : 8; uint32 Mantissa : 23; #endif } Components; float FloatValue; }; };
符号位S | 指数位E | 尾数位M | 指数偏移 | |
Float16 | 1 | 5 | 10 | 15 |
Float32 | 1 | 8 | 23 | 127 |
Double | 1 | 11 | 52 | 1023 |