Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hexadecimal floating constant in C

Tags:

c

hex

constants

0x0.3p10 represents what value?

And what's the meaning of the p in the statement above?

like image 243
Nano HE Avatar asked Jan 28 '11 07:01

Nano HE


People also ask

What is float constant in C?

A "floating-point constant" is a decimal number that represents a signed real number. The representation of a signed real number includes an integer portion, a fractional portion, and an exponent. Use floating-point constants to represent floating-point values that can't be changed.

How do you represent a floating-point in hexadecimal?

C99 supports floating-point numbers that can be written in hexadecimal format. In hexadecimal format the exponent is a decimal number that indicates the power of two by which the significant part is multiplied. Therefore 0x1. fp3= 1.9375*8=1.55e1 .

Is hexadecimal a float?

A hexadecimal floating-point constant is shorthand for binary scientific notation, which is an abstract — yet direct — representation of a binary floating-point number.


1 Answers

0x0.3p10 is an example of a hexadecimal floating point literal, introduced in C99(1). The p separates the base number from the exponent.

The 0x0.3 bit is called the significand part (whole with optional fraction) and the exponent is the power of two by which it is scaled.

That particular value is calculated as 0.3 in hex, or 3 * 16-1 (3/16) multiplied by 210 (1024), which gives 3 * 1024 / 16 or 192. The following program confirms this:

#include <stdio.h> int main (void) {     double d = 0x0.3p10;     printf ("%.f\n", d); // Outputs 192     return 0; } 

Section 6.4.4.2 Floating constants of C99 has all the details:

A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The components of the significand part may include a digit sequence representing the whole-number part, followed by a period ., followed by a digit sequence representing the fraction part.

The components of the exponent part are an e, E, p, or P followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part has to be present; for decimal floating constants, either the period or the exponent part has to be present.

The significand part is interpreted as a (decimal or hexadecimal) rational number; the digit sequence in the exponent part is interpreted as a decimal integer. For decimal floating constants, the exponent indicates the power of 10 by which the significand part is to be scaled. For hexadecimal floating constants, the exponent indicates the power of 2 by which the significand part is to be scaled.

For decimal floating constants, and also for hexadecimal floating constants when FLT_RADIX is not a power of 2, the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner. For hexadecimal floating constants when FLT_RADIX is a power of 2, the result is correctly rounded.


(1) These literals were also added to C++ in the C++17 iteration of the standard, though the ability to format and parse them was part of C++11.

like image 80
paxdiablo Avatar answered Oct 09 '22 08:10

paxdiablo