Floating-Point Constant with Payload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Sargeant

    Floating-Point Constant with Payload

    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 "initialize r 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
  • Barry Schwarz

    #2
    Re: Floating-Point Constant with Payload

    On Sun, 20 Apr 2008 09:49:01 -0700, Jon Sargeant <delta17@cox.ne t>
    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 "initialize r 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

    Comment

    Working...