转换十六进制浮点常数 [英] Convert Hexadecimal Floating-Point Constant
问题描述
我有一些要转换的常量,它们的定义如下:
I have some constants to convert, they are defined as follow :
0x1.0p-126f
0x1.0p-126f
0x1.fffffep127f
0x1.fffffep127f
0x1.0p-23f
0x1.0p-23f
如何准确转换?
顺便说一句,这是因为Visual Studio不支持该表示法!!
BTW, it is because Visual Studio does not support this notation !!
推荐答案
在不符合C99的编译器中,您可以编写:
In a non-C99 compliant compiler, you can write:
0x1.0p-126f -> ldexp (1.0, -126)
0x1.fffffep127f -> ldexp(0x1fffffe, 127 - 4 * 6)
0x1.0p-23f -> ldexp(1.0, -23)
在第二个示例中, 6
是点后的十六进制数字的数量.原始文件具有 0x1.fffffe
,因此当我写整数 0x1fffffe
时,我写出的数字对于从后移出的每个数字要大2 4 倍指向该点之前的点,应使用第二个参数进行补偿.
In the second example, the 6
is the number of hexadecimal digits that were after the dot. The original had 0x1.fffffe
so when I wrote the integer 0x1fffffe
, I wrote a quantity 24 times larger for each digit I shifted from after the point to before the point, which should be compensated for with the second argument.
函数 ldexp()
是标准的:
简介
#include <math.h>
double
ldexp(double x, int n);
说明
The ldexp() functions multiply x by 2 to the power n.
如果这些表达式出现在需要常量表达式的地方,则函数调用不是可接受的替代方法.在这种情况下,只需使用C程序以%.16e
格式打印 ldexp()
调用的结果,然后使用打印的值代替原始值.
If these expressions appear in a place where constant expressions are expected, then function calls are not an acceptable substitute.
In this case, simply print the result of the ldexp()
calls with a C program and the format %.16e
, and use the printed value in substitution for the original.
这篇关于转换十六进制浮点常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!