Floating-Point Constant with Payload

J

Jon Sargeant

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". 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?

Thanks,
Jon
 
B

Barry Schwarz

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". 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?

Answered in alt.comp.lang.c. Please don't post separate messages to
multiple newsgroups. If you want wide dissemination, post a single
message to all the groups at once (using multiple addresses).


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top