Hexadecimal floating-point constants in C

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I ve got a hexadecimal floating-point constant which I d like to declare directly in my C program, and avoid conversion. I believe it must be normalized first, right? How do I normalize it and declare it?

// hex constant 0xDE.488631  
double val = 0xDE.488631; // Error must have exponent.
double val = 0x0.DE488631p-2;  // Pretty sure this is wrong.

Answers

You can use an exponent of 0:

float val = 0xDE.488641p0;

Which in more normal looking notation means DE.488641×20 (in base 16, of course). Your guess was close - the exponent is a binary exponent, not a hex exponent, though. You re also using a negative exponent when you want to have a positive exponent. Correcting your second example, you can use:

float val = 0x0.DE488631p8;

Which in regular mathematical notation means 0.DE488631×28, or equivalently with a hexadecimal base for the exponent, 0.DE488631×162.

I think using an exponent of 0 is a lot easier to understand unless you have some reason to use the form from your second example.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/5480319/hexadecimal-floating-point-constants-in-c

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils