Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: OxCp syntax ? (http://www.velocityreviews.com/forums/t951488-re-oxcp-syntax.html)

 James Kuyper 08-28-2012 01:00 PM

Re: OxCp syntax ?

On 08/28/2012 07:28 AM, Guillaume Dargaud wrote:
> Hello all,
> I just ran across this syntax:
> #define NAN 0xCp125f
> Can somebody explain ? A quick googling turned out nothing.

C99 added the capability of specifying floating point constants using
constants and decimal floating point constants. The 0x is the
to a decimal value of 13. 'p' is the separator, corresponding to "e" in
decimal floating point constants. 125 is the exponent. "f" is a suffix
indicating that this constant has the type 'float'. This particular
constant represents a float value of 0xC*2^125. Compare to 5e60, which
represents 5*10^60. Fractional parts are also allowed, but unlike
decimal floating point constants, the binary exponent part cannot be
dropped:

#define PI 0x3.243F6A88p0

One of the most important characteristics of hexadecimal floating point
constants is explained in 6.4.4.2p3:
> ... 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. ...

Almost all real-world implementations of C have a power of 2 for
point value gives you more precise control over the value that it
represents than a decimal floating point value would.

Note: it's not clear to me that, on a system which supports true NaNs,
that 0xCp125f could be a NaN. If 0xC*2^125 is representable, then by
definition it isn't a NaN. If it is not representable, then a diagnostic
is required (the behavior being undefined at that point, it would be
permissible for a true NaN to be generated). However, if you ran into
this definition inside of <math.h>, then I might be wrong about that.
--
James Kuyper

 Eric Sosman 08-28-2012 01:10 PM

Re: OxCp syntax ?

On 8/28/2012 9:00 AM, James Kuyper wrote:
> [...]"C" is the hexadecimal digit sequence, corresponding
> to a decimal value of 13. [...]

Baker's hex?

--
Eric Sosman
esosman@ieee-dot-org.invalid

 James Kuyper 08-28-2012 01:58 PM

Re: OxCp syntax ?

On 08/28/2012 09:10 AM, Eric Sosman wrote:
> On 8/28/2012 9:00 AM, James Kuyper wrote:
>> [...]"C" is the hexadecimal digit sequence, corresponding
>> to a decimal value of 13. [...]

>
> Baker's hex?

:-} I'll use that excuse.
--
James Kuyper

 All times are GMT. The time now is 06:36 AM.