简体   繁体   English

十六进制浮点常数

[英]Hexadecimal Floating-Point Constant

In the following C program the usage of hexadecimal floating-point constants is demonstrate 在下面的C程序中,演示了十六进制浮点常量的用法

double d;
d = 2;
 printf("Ex 1: 2 in hex: %a\n\n",d);

 d = 256;
 printf("Ex 2: 2^8 in hex: %a\n\n",d);

 d = 0.015625; //= 2^-6
 printf("Ex 3: 2^-6 in hex: %a\n\n",d);

 d = 0.857421875;
 printf("Ex 4: 0.857421875 in hex: %a\n\n",d);

the results are: 结果是:

Ex 1: 2 in hex: 0x1p+1

Ex 2: 2^8 in hex: 0x1p+8

Ex 3: 2^-6 in hex: 0x1p-6

Ex 4: 0.857421875 in hex: 0x1.b7p-1

I don`t understand how one get the result for ex 4? 我不明白如何获得ex 4的结果?

The mantissa in the floating point number is typically represented with a number bigger or equal than 1 but smaller than 2. It's exactly equal to 1 in all examples but the last one. 浮点数中的尾数通常用大于或等于1但小于2的数字表示。在所有示例中,除最后一个数字外,尾数均等于1。

So, what do you do with the last one? 那么,您如何处理最后一个? Rerepresent 0.857421875 as 0.857421875 * 2/2, that is, as 1.71484375*2 -1 . 将0.857421875表示为0.857421875 * 2/2,即1.71484375 * 2 -1 Now, you want the mantissa in hex. 现在,您想要十六进制的尾数。 Double has 53 bits in the mantissa (including the implied 1.), so 1.71484375 in those 52(53) bits is effectively represented as an integer equal to 1.71484375*2 52 = 7722969673498624 = 0x1B700000000000. Double在尾数中有53位(包括隐含的1),因此这52(53)位中的1.71484375有效地表示为等于1.71484375 * 2 52 = 7722969673498624 = 0x1B700000000000的整数。 So, there you have it, 0x1.b7p-1. 所以,那里就是0x1.b7p-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM