Spectral
Spectral

Reputation: 717

Convert Hexadecimal Floating-Point Constant

I have some constants to convert, they are defined as follow :

0x1.0p-126f

0x1.fffffep127f

0x1.0p-23f

How can I convert them accurately ?

BTW, it is because Visual Studio does not support this notation !!

Upvotes: 1

Views: 922

Answers (1)

Pascal Cuoq
Pascal Cuoq

Reputation: 80355

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)

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.

Function ldexp() is standard:

SYNOPSIS

 #include <math.h>

 double
 ldexp(double x, int n);

DESCRIPTION

 The ldexp() functions multiply x by 2 to the power n.

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.

Upvotes: 2

Related Questions