|
|
Up |
|
|
  |
Author: Jon SargeantJon Sargeant Date: Apr 19, 2008 22:14
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?
Thanks,
Jon
|
| |
|
| | 15 Comments |
|
  |
Author: Barry SchwarzBarry Schwarz Date: Apr 20, 2008 11:28
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.
|
| Show full article (0.85Kb) |
|
| | no comments |
|
  |
Author: Jon SargeantJon 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. ...
|
| Show full article (1.31Kb) |
| 1 Comment |
|
  |
Author: Barry SchwarzBarry Schwarz Date: Apr 20, 2008 17:07
On Sun, 20 Apr 2008 16:09:29 -0700, Jon Sargeant cox.net>
wrote:
>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...
|
| Show full article (1.48Kb) |
| no comments |
|
  |
Author: Jon SargeantJon Sargeant Date: Apr 20, 2008 17:21
Barry Schwarz wrote:
> On Sun, 20 Apr 2008 16:09:29 -0700, Jon Sargeant cox.net>
> wrote:
>
>> 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 ...
|
| Show full article (1.61Kb) |
| no comments |
|
  |
Author: Barry SchwarzBarry Schwarz Date: Apr 21, 2008 04:50
On Sun, 20 Apr 2008 17:21:51 -0700, Jon Sargeant cox.net>
wrote:
>Barry Schwarz wrote:
>> On Sun, 20 Apr 2008 16:09:29 -0700, Jon Sargeant cox.net>
>> wrote:
>>
>>> Barry Schwarz wrote:
>>>> On Sat, 19 Apr 2008 22:14:01 -0700, Jon Sargeant cox.net...
|
| Show full article (1.84Kb) |
| no comments |
|
  |
Author: mimusmimus Date: Apr 21, 2008 11:02
On Sat, 19 Apr 2008 22:14:01 -0700, Jon Sargeant 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?
Declaring your unsigned and a float-pointer set (cast) to that unsigned
won't work?
--
C: The First and Still the Best Portable AL!
|
| Show full article (0.87Kb) |
| no comments |
|
  |
Author: Jon SargeantJon Sargeant Date: Apr 21, 2008 16:45
Barry Schwarz wrote:
> On Sun, 20 Apr 2008 17:21:51 -0700, Jon Sargeant cox.net>
> wrote:
>
>> Barry Schwarz wrote:
>>> On Sun, 20 Apr 2008 16:09:29 -0700, Jon Sargeant cox.net>
>>> wrote:
>>>
>>>> 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
>>>>>> ...
|
| Show full article (2.63Kb) |
| no comments |
|
  |
Author: Barry SchwarzBarry Schwarz Date: Apr 21, 2008 19:13
On Mon, 21 Apr 2008 16:45:17 -0700, Jon Sargeant cox.net>
wrote:
>Barry Schwarz wrote:
>> On Sun, 20 Apr 2008 17:21:51 -0700, Jon Sargeant cox.net>
>> wrote:
>>
>>> Barry Schwarz wrote:
>>>> On Sun, 20 Apr 2008 16:09:29 -0700, Jon Sargeant cox.net...
|
| Show full article (3.71Kb) |
| no comments |
|
  |
|
|
  |
Author: brix99luftballonsbrix99luftballons Date: Apr 22, 2008 05:52
Jon Sargeant ha scritto:
>>> Perhaps a more interesting example will help illustrate my objective:
>>>
>>> struct Vector
>>> {
>>> float x;
>>> float y;
>>> float z;
>>> };
>>> union U { unsigned int u; float f; };
>>> const union U u={0x3f800000U};
>>> const Vector vector={u.f,u.f,u.f}; //error: initializer element is
>>> not constant
>>
If you use a pointer:
const unsigned int x= 0x3f800000U; //assume 32-bits
const float* pY= (float*)&x ; //assume 32-bit single-precision
IEEE754
|
| Show full article (0.53Kb) |
| no comments |
|
|
|
|
|
|