Re: Floating-Point Constant with Payload
  Home FAQ Contact Sign in
alt.comp.lang.c only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Floating-Point Constant with Payload         

Group: alt.comp.lang.c · Group Profile
Author: Jon Sargeant
Date: Apr 20, 2008 16:09

Barry Schwarz wrote:
> On Sat, 19 Apr 2008 22:14:01 -0700, Jon Sargeant cox.net>
> wrote:
>
>> Hi,
>>
>> I'm looking for a general purpose way of initializing a floating-point
>> constant with an arbitrary bit pattern using C99. For example,
>>
>> const unsigned int x=0x3f800000U; //assume 32-bits
>> const float y=*(float*)&x; //assume 32-bit single-precision IEEE754
>>
>> Compiling this code on GCC generates the error "initializer element is
>> not constant". C99 hex floats are a partial solution. I can enter
>> plus/minus zero, finites, infinites (using 1.0/0.0 and -1.0/0.0), but
>> NaNs have me stumped. How do I get a constant NaN with a specific
>> payload? nan() looked promising until I realized that it returns a
>> variable. Any ideas?
>>
> You could try a union.
>
> Is y declared at file scope? If so, you might try moving it into a
> function.
>
>
> Remove del for email

Sorry about cross posting. I meant to post the message to comp.lang.c,
but posted it to alt.comp.lang.c by mistake. I tried a union but it
doesn't work:

union U { unsigned int u; float f; };
const union U u={0x3f800000U};
const float f=u.f;

GCC complains with the message "initializer element is
not constant". The floating-point constant needs to be global.

Best Regards,
Jon
1 Comment
diggit! del.icio.us! reddit!