f



Portably replacing -0.0 with 0.0

So my program has the following function at the moment to replace -0.0 with +0.0
in a function argument:

static inline double
sign_flat(double v)
{
  if (v == 0.0)
    return 0.0;
  return v;
}

But when compiling with the Intel Compiler icc at optimization level -O2 or
higher the above still produces -0.0. This raises two questions:

1. is the compiler free to convert above function into a NOP?

2. does anyone know a work-around or even more suitable formulation to achieve
the same? Preferably not some integer operations using signbit since our main
system is a Power6 which has some issues with FPU depending on ALU results and
vice versa. I've done some searching but the Internet is flooded with people
trying to wrap their head around the fact that -0.0 exists.

1. is actually the less interesting question since even when the answer was no
I still want my code to work with the Intel compiler.

Regards, Thomas
0
jahns (60)
7/19/2013 1:35:45 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

21 Replies
1180 Views

Similar Articles

[PageSpeed] 34

On 07/19/2013 03:35 PM, Thomas Jahns wrote:
> static inline double
> sign_flat(double v)
> {
>    if (v == 0.0)

Did you mean `if (v == -0.0)' ?

(yes, this is not the same in IEEE754 ; see 
http://en.wikipedia.org/wiki/Signed_zero)

0
xroche (207)
7/19/2013 2:13:04 PM
On 7/19/2013 9:35 AM, Thomas Jahns wrote:
> So my program has the following function at the moment to replace -0.0 with +0.0
> in a function argument:
>
> static inline double
> sign_flat(double v)
> {
>    if (v == 0.0)
>      return 0.0;
>    return v;
> }
>
> But when compiling with the Intel Compiler icc at optimization level -O2 or
> higher the above still produces -0.0. This raises two questions:
>
> 1. is the compiler free to convert above function into a NOP?

     I am not a language lawyer, but I think not.  5.2.4.2.2p2
tells us that the value of "minus zero" is zero, and since zero
is equal to zero the `if' should be satisfied if `v' is of either
form.

> 2. does anyone know a work-around or even more suitable formulation to achieve
> the same? Preferably not some integer operations using signbit since our main
> system is a Power6 which has some issues with FPU depending on ALU results and
> vice versa. I've done some searching but the Internet is flooded with people
> trying to wrap their head around the fact that -0.0 exists.

     A couple things you might try:

	return v + 0.0;  // Hey, it *might* work ...

	if (v >= 0.0)  // Should be true for minus zero
	    v = copysign(v, 1.0);
	return v;

Since we already suspect an erroneous implementation, though,
there are few guarantees.  Maybe an Intel forum would have more
information.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid
0
esosman2045 (613)
7/19/2013 2:28:46 PM
On 7/19/2013 10:28 AM, Eric Sosman wrote:
> On 7/19/2013 9:35 AM, Thomas Jahns wrote:
>> So my program has the following function at the moment to replace -0.0
>> with +0.0
>> in a function argument:
>>  [...]

     Another thought just occurred to me: How certain are you
that the problematic value is actually minus zero, and not
some small-magnitude non-zero negative number?

	double v = -DBL_MIN;
	printf("v = %.25f\n", v);

.... should print `v = -0.0000000000000000000000000'.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid
0
esosman2045 (613)
7/19/2013 2:35:10 PM
On 07/19/2013 10:13 AM, Xavier Roche wrote:
> On 07/19/2013 03:35 PM, Thomas Jahns wrote:
>> static inline double
>> sign_flat(double v)
>> {
>>    if (v == 0.0)
> 
> Did you mean `if (v == -0.0)' ?
> 
> (yes, this is not the same in IEEE754 ; see 
> http://en.wikipedia.org/wiki/Signed_zero)

It doesn't make any difference; as it says on that page: "According to
the IEEE 754 standard, negative zero and positive zero should compare as
equal with the usual (numerical) comparison operators, like the ==
operators of C and Java."

To determine whether v is a negative zero, you must use if(v==0.0 &&
copysign(1.0,v)<0.0). However, in this context that's just a waste of
time, if v is a positive zero, it doesn't matter whether sign_flat()
returns v or 0.0, so there's no need to distinguish it from a negative zero.
0
jameskuyper (5635)
7/19/2013 3:13:56 PM
Thomas Jahns <jahns@idontlikespam.dkrz.de> wrote:

> So my program has the following function at the moment to 
> replace -0.0 with +0.0
> in a function argument:
 
> static inline double
> sign_flat(double v)
> {
>  if (v == 0.0)
>    return 0.0;
>  return v;
> }
 
> But when compiling with the Intel Compiler icc at optimization 
> level -O2 or higher the above still produces -0.0. 
> This raises two questions:
 
> 1. is the compiler free to convert above function into a NOP?

As well as I know it, it is usual for compilers to be less 
strict on floating point rules at higher optimization levels.

One answer is to use -O0 to turn off such optimizations.

I believe it is legal C to convert to a no-op, but maybe not the
strict IEEE floating point rules. But C doesn't require IEEE
floating point, or strict adherence to it. Your compiler may offer
that as an option.

It might help to know why your are trying to get rid of -0.0.
Note that it is required to compare equal using comparison
operators.

-- glen

0
gah (12851)
7/19/2013 4:28:33 PM
Thomas Jahns <jahns@idontlikespam.dkrz.de> writes:

> So my program has the following function at the moment to
> replace -0.0 with +0.0 in a function argument:
>
> static inline double
> sign_flat(double v)
> {
>   if (v == 0.0)
>     return 0.0;
>   return v;
> }
>
> But when compiling with the Intel Compiler icc at optimization
> level -O2 or higher the above still produces -0.0.  This raises
> two questions:
>
> 1. is the compiler free to convert above function into a NOP?

Strictly speaking, no, assuming the implementation supports signed
zeros.  On implementations that do not have signed zeros, this
function may be compiled as if it were the identity function (only
I can't tell from the Standard whether signaling NaN's might be an
exception to that).

> 2. does anyone know a work-around or even more suitable formulation
> to achieve the same?  Preferably not some integer operations using
> signbit since our main system is a Power6 which has some issues
> with FPU depending on ALU results and vice versa.  I've done some
> searching but the Internet is flooded with people trying to wrap
> their head around the fact that -0.0 exists.

If the implementation isn't conforming, you're stuck (but see
also below).

If the implementation has only unsigned zeros, then most likely
problems with "negative zero" aren't going to come up, but if
they do I don't think there's anything to be done about it,
because of 5.2.4.2.2 p4.

I'm pretty sure the Standard is meant to allow only signed floating
point zeros or unsigned floating point zeros, not both (ie, in any
particular implementation).  If it is allowed to have both, and one
is unfortunate enough to have deal with such a beast, then all I
can say is, Good luck on that! :)

Getting back to your quesstion..  if <math.h> is available,
you could use the copysign() function:

    double
    sign_flat_A( double x ){
        return  x ? x : copysign( x, 1. );
    }

If <math.h> isn't available, but the implementation supports
complex types, you can make use of real-to-complex conversion rules
to get an other-than-negative zero:

    double
    sign_flat_B( double x ){
        return  x ? x : (union {_Complex double z; double d[2];}){ 0.0 }.d[1];
    }

If <math.h> isn't available, and the implementation doesn't support
complex types, the last resort is to make use of initialization
rules to produce an other-than-negative zero:

    double
    sign_flat_C( double x ){
         extern const double const_double_zero;
         return  x ? x : const_double_zero;
    }

    /* ... and somewhere ... */

    const double const_double_zero;  /* positive or unsigned zero! */

This last method is probably the most reliable.  Too bad
it's also likely the slowest.

Incidentally, I tried Eric Sosman's suggestion of using

    return  x + 0.0;

and was amused to see that it worked (on my system here,
naturally), but that

    return  x - 0.0;

did not.  A downside of approaches like this is that they might
have unexpected behaviors when dealing with infinities or NaN's
(especially signaling NaN's).
0
txr1 (1467)
7/19/2013 5:53:47 PM
Eric Sosman <esosman@comcast-dot-net.invalid> writes:

> On 7/19/2013 9:35 AM, Thomas Jahns wrote:
>> So my program has the following function at the moment to
>> replace -0.0 with +0.0 in a function argument:
>>
>> static inline double
>> sign_flat(double v)
>> {
>>    if (v == 0.0)
>>      return 0.0;
>>    return v;
>> }
>>
>> But when compiling with the Intel Compiler icc at optimization
>> level -O2 or higher the above still produces -0.0. This raises
>> two questions:
>>
>> 1. is the compiler free to convert above function into a NOP?
>
>     I am not a language lawyer, but I think not.  5.2.4.2.2p2
> tells us that the value of "minus zero" is zero, and since zero
> is equal to zero the `if' should be satisfied if `v' is of either
> form.

I agree with the conclusion, but not the reasoning (specifically
the reasoning about why negative zero must == positive zero).
The text in 5.2.4.2.2 describes the characteristics of floating
point types to be able to talk about what ranges of values they
can represent, but it doesn't deal with how operations work.  If
'z' is a positive zero and 'n' is a negative zero, then 'z == n'
is true because the mathematical values are equal, ie, 0 and -0
are the no different when considered as real numbers.  The
equality of positive zero and negative zero has to hold whether
or not floating point numbers are represented in the form of
5.2.4.2.2p2, which indeed they may not be.  The model is used to
describe the range of values, but it doesn't define the values.
0
txr1 (1467)
7/19/2013 6:08:00 PM
Tim Rentsch <txr@alumni.caltech.edu> wrote:

(snip on negative zero)

> I agree with the conclusion, but not the reasoning (specifically
> the reasoning about why negative zero must == positive zero).
> The text in 5.2.4.2.2 describes the characteristics of floating
> point types to be able to talk about what ranges of values they
> can represent, but it doesn't deal with how operations work.  If
> 'z' is a positive zero and 'n' is a negative zero, then 'z == n'
> is true because the mathematical values are equal, ie, 0 and -0
> are the no different when considered as real numbers.  The
> equality of positive zero and negative zero has to hold whether
> or not floating point numbers are represented in the form of
> 5.2.4.2.2p2, which indeed they may not be.  The model is used to
> describe the range of values, but it doesn't define the values.

The other reason is that users expect it.

As far as I know, there are two ways of dealing with negative
zero in hardware, both for floating point and non-twos-complement
fixed point:

  1) Always compare them equal.
  2) Never generate negative zero as the result of an
     arithmetic operation.

In the latter case, if you generate one, for example using
bitwise operators, it may propagate and cause surprising results.

The former case will surprise users of bitwise operators, such
as  ~x==x being true for x==0 on ones complement hardware.

-- glen

0
gah (12851)
7/19/2013 7:18:04 PM
glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:

> Tim Rentsch <txr@alumni.caltech.edu> wrote:
>
> (snip on negative zero)
>
>> I agree with the conclusion, but not the reasoning (specifically
>> the reasoning about why negative zero must == positive zero).
>> The text in 5.2.4.2.2 describes the characteristics of floating
>> point types to be able to talk about what ranges of values they
>> can represent, but it doesn't deal with how operations work.  If
>> 'z' is a positive zero and 'n' is a negative zero, then 'z == n'
>> is true because the mathematical values are equal, ie, 0 and -0
>> are the no different when considered as real numbers.  The
>> equality of positive zero and negative zero has to hold whether
>> or not floating point numbers are represented in the form of
>> 5.2.4.2.2p2, which indeed they may not be.  The model is used to
>> describe the range of values, but it doesn't define the values.
>
> The other reason is that users expect it.  [snip]

It is a reason, but it's a reason for an answer to
a different question.
0
txr1 (1467)
7/19/2013 8:43:56 PM
On Fri, 19 Jul 2013 13:35:45 UTC, Thomas Jahns <jahns@idontlikespam.dkrz.de> wrote:

> So my program has the following function at the moment to replace -0.0 with +0.0
> in a function argument:
> 
> static inline double
> sign_flat(double v)
> {
>   if (v == 0.0)
>     return 0.0;
>   return v;
> }
> 
> But when compiling with the Intel Compiler icc at optimization level -O2 or
> higher the above still produces -0.0. This raises two questions:
> 
> 2. does anyone know a work-around or even more suitable formulation to achieve
> the same? Preferably not some integer operations using signbit since our main
> system is a Power6 which has some issues with FPU depending on ALU results and
> vice versa. I've done some searching but the Internet is flooded with people
> trying to wrap their head around the fact that -0.0 exists.

Several things to try:
 add 'volatile' to 'double v'
 change 'return 0.0' to 'return fabs(v)' or 'v = fabs(v)'

My experience in testing many C compilers is adding 'volatile' is the only way to
turn off "optimizations".
---
Fred J. Tydeman        Tydeman Consulting
tydeman@tybor.com      Testing, numerics, programming
+1 (775) 287-5904      Vice-chair of PL22.11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.
0
7/20/2013 12:59:56 AM
On Fri, 19 Jul 2013 11:08:00 -0700, Tim Rentsch
<txr@alumni.caltech.edu> wrote:

>Eric Sosman <esosman@comcast-dot-net.invalid> writes:
>
>> On 7/19/2013 9:35 AM, Thomas Jahns wrote:
>>> So my program has the following function at the moment to
>>> replace -0.0 with +0.0 in a function argument:
>>>
>>> static inline double
>>> sign_flat(double v)
>>> {
>>>    if (v == 0.0)
>>>      return 0.0;
>>>    return v;
>>> }
>>>
>>> But when compiling with the Intel Compiler icc at optimization
>>> level -O2 or higher the above still produces -0.0. This raises
>>> two questions:
>>>
>>> 1. is the compiler free to convert above function into a NOP?
>>
>>     I am not a language lawyer, but I think not.  5.2.4.2.2p2
>> tells us that the value of "minus zero" is zero, and since zero
>> is equal to zero the `if' should be satisfied if `v' is of either
>> form.
>
>I agree with the conclusion, but not the reasoning (specifically
>the reasoning about why negative zero must == positive zero).
>The text in 5.2.4.2.2 describes the characteristics of floating
>point types to be able to talk about what ranges of values they
>can represent, but it doesn't deal with how operations work.  If
>'z' is a positive zero and 'n' is a negative zero, then 'z == n'
>is true because the mathematical values are equal, ie, 0 and -0
>are the no different when considered as real numbers.  The
>equality of positive zero and negative zero has to hold whether
>or not floating point numbers are represented in the form of
>5.2.4.2.2p2, which indeed they may not be.  The model is used to
>describe the range of values, but it doesn't define the values.


I believe a somewhat similar situation arises on implementations that
support unnormalized* FP numbers.  You can then have multiple bit
patterns for the same number (for example, you might have 1.2E+3 and
0.12E+4), but they must compare equal.


*Not to be confused with IEEE denormals.
0
robertwessel2 (1674)
7/20/2013 6:07:01 AM
Robert Wessel <robertwessel2@yahoo.com> writes:

> On Fri, 19 Jul 2013 11:08:00 -0700, Tim Rentsch
> <txr@alumni.caltech.edu> wrote:
>
>>Eric Sosman <esosman@comcast-dot-net.invalid> writes:
>>
>>> On 7/19/2013 9:35 AM, Thomas Jahns wrote:
>>>> So my program has the following function at the moment to
>>>> replace -0.0 with +0.0 in a function argument:
>>>>
>>>> static inline double
>>>> sign_flat(double v)
>>>> {
>>>>    if (v == 0.0)
>>>>      return 0.0;
>>>>    return v;
>>>> }
>>>>
>>>> But when compiling with the Intel Compiler icc at optimization
>>>> level -O2 or higher the above still produces -0.0. This raises
>>>> two questions:
>>>>
>>>> 1. is the compiler free to convert above function into a NOP?
>>>
>>>     I am not a language lawyer, but I think not.  5.2.4.2.2p2
>>> tells us that the value of "minus zero" is zero, and since zero
>>> is equal to zero the `if' should be satisfied if `v' is of either
>>> form.
>>
>> I agree with the conclusion, but not the reasoning (specifically
>> the reasoning about why negative zero must == positive zero).
>> The text in 5.2.4.2.2 describes the characteristics of floating
>> point types to be able to talk about what ranges of values they
>> can represent, but it doesn't deal with how operations work.  If
>> 'z' is a positive zero and 'n' is a negative zero, then 'z == n'
>> is true because the mathematical values are equal, ie, 0 and -0
>> are the no different when considered as real numbers.  The
>> equality of positive zero and negative zero has to hold whether
>> or not floating point numbers are represented in the form of
>> 5.2.4.2.2p2, which indeed they may not be.  The model is used to
>> describe the range of values, but it doesn't define the values.
>
>
> I believe a somewhat similar situation arises on implementations that
> support unnormalized* FP numbers.  You can then have multiple bit
> patterns for the same number (for example, you might have 1.2E+3 and
> 0.12E+4), but they must compare equal.

Right, this case is exactly analogous.

> *Not to be confused with IEEE denormals.

The Standard calls such things 'subnormal floating-point values'
(or just 'subnormals').  As of 2008, IEEE 754-2008 also uses
the term 'subnormal' rather than 'denormal'.
0
txr1 (1467)
7/20/2013 3:12:47 PM
On 07/19/13 16:35, Eric Sosman wrote:
>     Another thought just occurred to me: How certain are you
> that the problematic value is actually minus zero, and not
> some small-magnitude non-zero negative number?

I already checked that in the debugger before posting here.

Thomas

0
jahns (60)
7/22/2013 8:58:09 AM
On 07/19/13 15:35, Thomas Jahns wrote:
> 1. is the compiler free to convert above function into a NOP?

summarizing the input from various posters I guess no is the valid answer here,
but see below.

> 2. does anyone know a work-around or even more suitable formulation to achieve
> the same? Preferably not some integer operations using signbit since our main
> system is a Power6 which has some issues with FPU depending on ALU results and
> vice versa. I've done some searching but the Internet is flooded with people
> trying to wrap their head around the fact that -0.0 exists.

Turns out the Intel compiler actually does what I want, once I add

 -fp-model precise

to the compiler flags. One could probably argue no end why -fp-model fast=1 is
the default and why this is only another installment of me running into one or
another of the problems this default causes, but I guess one purpose of this
thread is to put out another bit of information for others suffering from
similar problems.

Regards, Thomas
0
jahns (60)
7/22/2013 9:11:00 AM
On 07/22/2013 05:11 AM, Thomas Jahns wrote:
> On 07/19/13 15:35, Thomas Jahns wrote:
>> 1. is the compiler free to convert above function into a NOP?
> 
> summarizing the input from various posters I guess no is the valid answer here,
> but see below.
> 
>> 2. does anyone know a work-around or even more suitable formulation to achieve
>> the same? Preferably not some integer operations using signbit since our main
>> system is a Power6 which has some issues with FPU depending on ALU results and
>> vice versa. I've done some searching but the Internet is flooded with people
>> trying to wrap their head around the fact that -0.0 exists.
> 
> Turns out the Intel compiler actually does what I want, once I add
> 
>  -fp-model precise
> 
> to the compiler flags. One could probably argue no end why -fp-model fast=1 is
> the default and why this is only another installment of me running into one or
> another of the problems this default causes, but I guess one purpose of this
> thread is to put out another bit of information for others suffering from
> similar problems.

I'm curious - you never explained why you needed to replace negative
zeroes with positive zeroes. There are certainly cases where it makes a
difference, but for the most part they're contrived situations, often
involving code that inappropriately magnifies small differences in an
input number into large differences in the results. For instance, the
difference between a positive and a negative zero can change the value
returned by atan2() by almost 2*pi. However, in most cases, code that
uses such results should treat angles that differ by almost 2*pi as
being, in fact, very close to each other.
What are you doing with these negative zeros that makes it important to
convert them to positive zeros?
-- 
James Kuyper
0
jameskuyper (5635)
7/22/2013 11:46:13 AM
On 07/22/13 13:46, James Kuyper wrote:
> I'm curious - you never explained why you needed to replace negative
> zeroes with positive zeroes. There are certainly cases where it makes a
> difference, but for the most part they're contrived situations, often
> involving code that inappropriately magnifies small differences in an
> input number into large differences in the results. For instance, the
> difference between a positive and a negative zero can change the value
> returned by atan2() by almost 2*pi. However, in most cases, code that
> uses such results should treat angles that differ by almost 2*pi as
> being, in fact, very close to each other.
> What are you doing with these negative zeros that makes it important to
> convert them to positive zeros?

I'm testing a floating point file storage format (GRIB) of which I know how many
bits etc. are preserved. I massage the double input to match this (i.e. cut off
extra precision) and write it to file. Afterwards I run a test if the contents
read in from the file still match the input. Since the file format library maps
negative zero to positive, I need to get rid of negative zero first, since the
comparison is byte-for-byte via checksum.

Thomas

0
jahns (60)
7/22/2013 12:49:39 PM
Thomas Jahns <jahns@idontlikespam.dkrz.de> writes:

> On 07/22/13 13:46, James Kuyper wrote:
>> I'm curious - you never explained why you needed to replace
>> negative zeroes with positive zeroes.  There are certainly cases
>> where it makes a difference, but for the most part they're
>> contrived situations, often involving code that inappropriately
>> magnifies small differences in an input number into large
>> differences in the results.  For instance, the difference between a
>> positive and a negative zero can change the value returned by
>> atan2() by almost 2*pi.  However, in most cases, code that uses
>> such results should treat angles that differ by almost 2*pi as
>> being, in fact, very close to each other.
>> What are you doing with these negative zeros that makes it
>> important to convert them to positive zeros?
>
> I'm testing a floating point file storage format (GRIB) of which I
> know how many bits etc. are preserved.  I massage the double input
> to match this (i.e. cut off extra precision) and write it to file.
> Afterwards I run a test if the contents read in from the file still
> match the input.  Since the file format library maps negative zero
> to positive, I need to get rid of negative zero first, since the
> comparison is byte-for-byte via checksum.

This suggests to me that you might want to use memcmp() and
memcpy() rather than floating point operations.
0
txr1 (1467)
7/22/2013 6:03:41 PM
On Monday, July 22, 2013 5:49:39 AM UTC-7, Thomas Jahns wrote:
> On 07/22/13 13:46, James Kuyper wrote: > I'm curious - you never explaine=
d why you needed to replace negative > zeroes with positive zeroes. There a=
re certainly cases where it makes a > difference, but for the most part the=
y're contrived situations, often > involving code that inappropriately magn=
ifies small differences in an > input number into large differences in the =
results. For instance, the > difference between a positive and a negative z=
ero can change the value > returned by atan2() by almost 2*pi. However, in =
most cases, code that > uses such results should treat angles that differ b=
y almost 2*pi as > being, in fact, very close to each other. > What are you=
 doing with these negative zeros that makes it important to > convert them =
to positive zeros? I'm testing a floating point file storage format (GRIB) =
of which I know how many bits etc. are preserved. I massage the double inpu=
t to match this (i.e. cut off extra precision) and write it to file. Afterw=
ards I run a test if the contents read in from the file still match the inp=
ut. Since the file format library maps negative zero to positive, I need to=
 get rid of negative zero first, since the comparison is byte-for-byte via =
checksum. Thomas

Why can't you use:
 if ( readInValue=3D=3D0 && internalValue=3D=3D0 ) {
    compare=3Dtrue;
 } else {
    compare =3D myNormalComparer( readInValue, internalValue );
 }

--=20
Fred K
0
7/22/2013 6:36:43 PM
Thomas Jahns <jahns@idontlikespam.dkrz.de> writes:

> So my program has the following function at the moment to replace -0.0 with +0.0
> in a function argument:
> 
> static inline double
> sign_flat(double v)
> {
>   if (v == 0.0)
>     return 0.0;
>   return v;
> }
> 
> But when compiling with the Intel Compiler icc at optimization level -O2 or
> higher the above still produces -0.0. This raises two questions:
> 
> 1. is the compiler free to convert above function into a NOP?

When it comes to floating point, there's way too much flexibility, alas.
However, the above looks like it has quite unambiguous abstract machine
behaviour that should be mimicked at all optimisation levels. "0.0 compares 
equal to -0.0" is not justification to conclude "0.0 is equivalent to -0.0", 
IMHO. Put this function in a separate module, and compile it with optimisation
reduced.

> 2. does anyone know a work-around or even more suitable formulation to achieve
> the same? Preferably not some integer operations using signbit since our main
> system is a Power6 which has some issues with FPU depending on ALU results and
> vice versa. I've done some searching but the Internet is flooded with people
> trying to wrap their head around the fact that -0.0 exists.

Nothing more weird about + and - 0.0s than there is about + and - infinities.

I think you'll just have to write something that's harder for it to optimise.
Have you tried constructs like ``if(v == -v) return -v;''

> 1. is actually the less interesting question since even when the answer was no
> I still want my code to work with the Intel compiler.

Go hunting in the optimisation flags for something that might disable this
optimisation.

Phil
-- 
If "law-abiding citizens have nothing to fear" from privacy-invading 
technologies and policies, then law-abiding governments should have
nothing to fear from whistleblowers.
0
7/23/2013 12:42:06 PM
On 7/23/2013 8:42 AM, Phil Carmody wrote:
> Thomas Jahns <jahns@idontlikespam.dkrz.de> writes:
>
>> So my program has the following function at the moment to replace -0.0 with +0.0
>> in a function argument:
>>
>> static inline double
>> sign_flat(double v)
>> {
>>    if (v == 0.0)
>>      return 0.0;
>>    return v;
>> }
>>
>> But when compiling with the Intel Compiler icc at optimization level -O2 or
>> higher the above still produces -0.0. This raises two questions:
>>
>> 1. is the compiler free to convert above function into a NOP?
>
> When it comes to floating point, there's way too much flexibility, alas.
> However, the above looks like it has quite unambiguous abstract machine
> behaviour that should be mimicked at all optimisation levels. "0.0 compares
> equal to -0.0" is not justification to conclude "0.0 is equivalent to -0.0",
> IMHO. Put this function in a separate module, and compile it with optimisation
> reduced.
[...]

Well, he wants it to be inlined if possible, so making a separate module 
precludes that ability.

Assuming that the others are correct that the original code can't be 
optimized to a no-op, then it might be a bug in the optimizer not 
special-casing 0.0 in:

     if ( foo == bar )
         return bar;
     else
         return foo;

Is there any reason not to make the code clearer in its intent, even if just 
to human readers, and make the comparison to negative zero?

   static inline double
   sign_flat(double v)
   {
      if (v == -0.0)
        return 0.0;
      return v;
   }

0
kenbrody (1879)
7/23/2013 3:44:14 PM
On 07/23/13 17:44, Ken Brody wrote:
> Assuming that the others are correct that the original code can't be optimized
> to a no-op, then it might be a bug in the optimizer not special-casing 0.0 in:

please read my follow-up: -fp-model precise fixes it.
0
jahns (60)
7/23/2013 4:36:02 PM
Reply:

Similar Artilces:

portable C, posix C and windows C
what is the difference between portable C, posix C and windows C ??? On Oct 7, 10:34=A0pm, asit <lipu...@gmail.com> wrote: > what is the difference between portable C, posix C and windows C ??? Portable means that it will compile on many platforms. To accomplish this, you need some kind of a standard. The ANSI/ISO C standard creates one kind of portability for C programs. Posix is another kind of standard. It allows some additional fascilities to be standardized that will not work without the Posix assumption. http://en.wikipedia.org/wiki/POSIX There is no such thing as Windows ...

What is the replacement in C++ for _aligned_malloc in C?
Hi, I want to allocate some memory 16Byte aligned. For this now I am using _aligned_malloc function that is given in malloc.h. What is the substitute for this in new ? Is there any way to allocate aligned memory using new ? Thanks and Regards, Amal P. Amal P <enjoyamalp@gmail.com> wrote: > I want to allocate some memory 16Byte aligned. For this now I am > using _aligned_malloc function that is given in malloc.h. What is the > substitute for this in new ? Is there any way to allocate aligned > memory using new ? _aligned_malloc is not a Standard function, but rather an extension provided by your implementation. You should probably continue using that extension function. regards -- jb (reply address in rot13, unscramble first) You can take a look at this usage of new called "placement new"... new (placement_addr) type; where placement_addr is compulsorily a pointer! But yes, you need to say delete placement_addr & not the type pointer used in "new". --with regards Neo ...

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta > * open source - preferable, but not 100% necessary what's wrong with using C? Friedrich > what's wrong with using C? Too low level, no built-in garbage collection. -- Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: >> what's wrong with using C? > > Too low level, Well the Smalltalk people say, have a small language and a big library. I assume C's library status beyond that of Smalltalk. > no built-in garbage collection. Use the Boehm Weisser GC. Friedrich "Kevin Albrecht" <kevin@albrecht.net> wrote in message news:<2TiYb.237892$rj7.1641209@twister.tampabay.rr.com>... > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) > ...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

Friedrich C-90 Replacement Pre-filter (C-90B and C-90A)
Price:$32.99 Image: http://megadiscountguru.info/image.php?id=B0002U35E6 Best deal: http://megadiscountguru.info/index.php?id=B0002U35E6 Replacement pre-filter for large particle filtration. This filter fits both the Friedrich C-90A and C-90B units. SIMILAR PRODUCTS: Friedrich CF Carbon Filter:http://megadiscountguru.info/index.php?id=B000BOJVLO Friedrich C-90B Electronic Air Purifier:http://megadiscountguru.info/index.php?id=B000GZ4FLO AT&amp;T 210 Trimline Phone with Memory Dialing (Black):http://megadiscountguru.info/index.php?id=B00005MITU LaCie 301371 500GB Rugged All...

portable complex numbers between C, C++
I've got a C library that I want to access from C++. I'd like to pass complex number portably between them. Right now, I've got code in the header file to define the argument according to C or C++. #ifdef __cplusplus # include <complex> typedef std::complex<float> float_complex; #else # include <complex.h> typedef float complex float_complex; #endif How portable is this? In article <1129821382.100246.198090@g47g2000cwa.googlegroups.com>, <mark@borgerding.net> wrote: >I've got a C library that I want to access from C++. I'...

Search and replace c-style strings in c++
I am trying to write a function that takes three c-style strings, and returns a c-style string. This function searches a c-string for all occurrences of the sub-string and replaces them with a different string. This program works but seems very inelegant. I can't help the feeling like it could have been done in a less bulky way. #include <iostream> #include <cstring> using namespace std; int main() { char* replaceSubstring(char*,char*,char*); char *string1, *string2, *string3; string1 = "the dog jumped over the fence"; string2 = "the"; string3 = "...

misc: C-AUX / C and binary portability
basically, this idea is related to an idea I am currently considering calling C-Aux, but is actually nothing terribly new in my case (ideas have been floating around for a while, and I already have a "header-free C" mode although its purpose and implementation details differ). technically, this is likely to be the default mode for compiling C to the BGBScript2 VM (implementation of this VM still ongoing, but I am at least making *some* progress). so, purpose: C-Aux will be a C variant (or C-like-language) capable of producing binary-portable code; it will aim for common-case...

[C / C++] Portable library to measure time
Is there any simple library that can return a systemtime in a resolution of max. 10ms, working on both Windows XP and modern Linux? time() works on both but has a resolution of 1s. There is GetSystsmTime on Windows with 10ms resolution, there are probably also some functions for Linux - but maybe there is some library works on both these OSes? This is a MIME GnuPG-signed message. If you see this text, it means that your E-mail or Usenet software does not support MIME signed messages. The Internet standard for MIME PGP messages, RFC 2015, was published in 1996. To open this message correctly ...

Microsoft C# a replacement for old C++ language
I heard from a friend that C++ is mainly being replaced as language of choice for most C++ programmers around the world. I guess this makes sence as it means that when learning you dont have to fuck around sencelessly with low level garbage like pointers. I was going to learn C++ but if times are changing I think I will just learn C#, it seems more modern. I also thought about Java but its WAY too slow. I think it should be easy to learn as I have heaps of experience making complex programs using all versions of Visual Basic. Any comparisons out there? Thanks The Grand Master Master Prog...

C as a Subset of C++ (or C++ as a superset of C)
Isn't it a lame use of human time and effort to maintain completely separate C and C++ standards? As in the words of Betty White about Facebook: "It seems like an incredible waste of time". Why don't the two standards groups get together and agree on a common specification for the ground which both standards cover? There would still be two separate standards, but they'd both be exactly the same for the common ground. The common ground document could be referred to by both standards instead of being maintained by both groups in individual efforts resulting in...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

c------->c++ and c++------>c
We can write c program in turbo c++ editor.But reverse is not true why? i.e The program will not compile if u write c++ program in turbo c editor vim wrote: > We can write c program in turbo c++ editor.But reverse is not true why? > i.e The program will not compile if u write c++ program in turbo c > editor This isn't about C; this is about some editor. Why the fircone should /we/ know? Ask in a relevant group. [And /why/, in an editor specialised to some specific programming languages, would you expect it to compile a /different/ one? Would you expect turbo perl to compi...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! Throne Software wrote: > Throne Software has opened up C, C++ and C# Forums at: > > http://www.thronesoftware.com/forum/ For all of your forums newsgroups already exist. > Join us! Why? Don't see any reason to support your marketing. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! In article <1120963701.862698.160260@g14g2000cwa.googlegroups.com>, Throne Software <mail@thronesoftware.com> writes >Throne Software has opened up C, C++ and C# Forums at: > >http://www.thronesoftware.com/forum/ > >Join us! Why? The beauty of news groups is that they come to me I don't have to go to them. Also all the people who have been around a while know what they are doing and are here. I noticed that there are some C and C++ Forums on another system. Full of the blind (opinionated) leading the blind. The problem is that as the experienced people who have been around a long while are on usenet. the system works. Why move. Certainly not to a forum on a commercial web site. The web site might go down. Usenet can't. Web based systems are not as safe. I have to trust you about cookies and all sorts. (active X etc). Usenet is international and independent. It is not controlled by anyone. Why would anyone move from this system to a web based one in a a rogue state? There is no point in running a web based forum except as a support forum for your own products. -- \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/ /\/\/ chris@phaedsys.org www.phaedsys.org \/\/\ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Chris Hills wrote: > In article <1120963701.862...

C,, C++, Java, C#
I come from C,C++,Java and C#. What can SmallTalk do for me and for what cost? Regarding the Squeak thing, it's interesting though confusing. Is it an IDE? Why are the menus so wacky? "DM McGowan II" <nospam@nospam.net> wrote in message news:PLednX2vnLd11IncRVn-rA@comcast.com... > I come from C,C++,Java and C#. What can SmallTalk do for me ...? Ask not what Smalltalk can do for, but what can you do with Smalltalk. :-) In very few words, it significantly reduces time to market, while enhancig the development experience. This is because: 1. It manages the memory for you, so you don't have to allocate/deallocate all the time (the source of some of the most nasty bugs in C). Java and C# learnt that feature from ST. 2. It has single inheritance. 3. All the development is in a single file, called the image, so you don't have to fish for where did you put that missing class. Yet, you don't have to go through thousands of lines of code to find the method you wrote three months ago. 4. It's the language for which it's easier to work within the Agile methodologies. 5. It's strongly typed without being unnecessarily redundant. 6. It's "wacky" interface releases you from the need to use curlys. 7. It comes with a huge library, so you don't need to reinvent the wheel as much. 8. It's debugger is highly integrated and friendly, so the experience of findin...

c,c++,obj-c
Of the 3 languages titled in this post is c++ older than obj-c ? Would someone experienced in C be able to dive right into C++ rather than obj-c ? Java is out for me now except applet programming I think I'm going to go with just C and C++. Bill Bill Cunningham wrote: > Of the 3 languages titled in this post is c++ older than obj-c ? Why does that matter? Do you want to use an old language? Try Fortran or LISP. > Would > someone experienced in C be able to dive right into C++ rather than > obj-c ? Not quite dive in, at least not into the OO parts of C++. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! ...

Java, C# replacing C++ as primary language for desktop development?
Hello, I'm not asking this to troll, I'm just genuinely interested in this question and I think that if I were to post it on a Java group that would be like asking the converted. Since C# has become Microsoft's flagship programming language, I'm wondering, does this mean that the sun is starting to set on C++ as the dominant language for, in particular, desktop development? Admittedly, I'm no expert in programming languages and the basis for my question could be inaccurate but this is the current picture of the situation as it looks to me. I think the reason for the shi...

c++/c
hi i have been recently told that there is no exception handling ic c+ +.?? as far as i remember i have read, it has. also java has exception handling for sure but there we can use finally also which we cant use in c ++. what other differences do we have in the two languages c++ and java in exception handling. and which one is better? rupika wrote: > > i have been recently told that there is no exception handling ic > c++.?? You should ask about C in comp.lang.c, about C++ in comp.lang.c++. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home...

More about C and C++
Hello... I have wrote: >I will resume it like this: >On the criterias of "complexity" and "difficulty", the C and C++ >programming languages are more complex and difficult than Object >Pascal for example, so since they are more complex and difficult they >are likely to create something like a darwinian filter who don't let >the weaker humans among us humans to cross or to climb the social >ladder, this is why i think that C and C++ do participate to social >darwinism, this is why i say that C and C++ are bad. Josep...

lang c++
Help for Graphics Program pic(numg); {Void Main() } raj wrote: > Help for Graphics Program > pic(numg); > {Void Main() > } result_type pic(numg); int main() { } regards Andy Little ...

c to c++
hi, i was wondering whether its worth converting from c to c++? i'm lazy but if its worth the effort... ______________________ http://monsterbeans.com/ | --------------------------------------- eminhanif@googlemail.com wrote: > hi, > i was wondering whether its worth converting from c to c++? > i'm lazy but if its worth the effort... Rather than participating in the language war flamefest your message invites, I will simply make the most obvious point: it is worth your while to learn any language that will be used in connection with your work. This includes learning a ...

Web resources about - Portably replacing -0.0 with 0.0 - comp.lang.c

Facebook Tests Replacing Keyword Ad Targeting With Broad Category Targeting
Facebook is testing a major functionality change for its self-serve performance ad tool that would replace the specific Like keyword targeting ...

The Dangers of Replacing Coil Springs on Your Vehicle - Bonus Clip - YouTube
In this episode of Saturday Mechanic bonus clip, Ben and Russ show what happens when a coil spring compressor fails. Subscribe today for more! ...

School apologises for taking cane off blind boy and replacing it with pool noodle
A primary school has apologised for taking away the cane of an eight-year-old blind boy and replacing it swimming pool noddle as a form of punishment. ...

Federation Centres stuns market by replacing CEO Steven Sewell with Angus McNaughton effective immediately ...
Retail landlord Federation Centres has surprised the market with the&nbsp;appointment of&nbsp;former Novion Property's Angus McNaughton as the ...

Replacing China's broken economic gauge
As Chinese growth shifts away from its reliance on heavy industry, we need a new index to the track the ups and downs of the economy.

Inner south residents worried about high-rises replacing public housing flats
Residents say the&nbsp;government&nbsp;is over-developing its inner south blocks to maximise their sale value.

Apache Software Foundation unveils Hadoop 2, replacing MapReduce with YARN
The Apache Software Foundation unveiled its latest release of its open source data processing program, Hadoop 2. It runs multiple applications ...

Australia spends $110 million a year replacing lost coins
Australians lose 255 million coins every year - down the back of our couches, taken overseas in the pockets of tourists and stuck to footpaths. ...

Tweets Replacing Resumes - Twitter Becomes Job-Hunting Tool
Forget paper resumes, your next job could hang on a 140-letter tweet.

A case of one bad government replacing another
THERE is a widening gap between the sort of government a Tony Abbott-led Coalition will need to become to steer the nation successfully in the ...

Resources last updated: 3/8/2016 6:54:48 AM