tags:

views:

40

answers:

3

Hello everyone,

My program seems to be producing some -0 values in place of values that should be 0. How do i stop my program from outputting these values?

Is it as simple as checking if a value is equal to -0 and reassigning it a zero instead? Or is there a more elegant solution?

+1  A: 

http://en.wikipedia.org/wiki/Signed_zero says: According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators.

I'd investigate whether the variables could be cast to a 32 bit int (for float) or 64 bit int (for double) and see if they're equal to 0x8000...

Arthur Kalliokoski
A: 

Sounds like you are using floating numbers. The -0 should be in fact a very small negative number like -0.0000001 which was truncated by your printing function. You can examine this by a good debugger, or print the raw float/double as hex bytes.

You can make some compare and set it to zero. (eg, if (n<0 && n > -0.0001) n = 0;)

(edit) Maybe you are doing things like this:

#include <stdio.h>
#include <float.h>
#include <iostream>

int main()
{
    double d = -DBL_MIN;
    std::cout << std::fixed << d << std::endl;
    return 0;
}

// output: -0.000000

Maybe you can try to print out the real value inside your float / double. That is,

cout << *(__int64)&d; // if d is double
cout << *(__int)&f;   // if f is float

Printing that helps to understand what happened.

Francis
No, I'm outputting data using floats directly from my RAM to HD in binary format (everything is preserved, nothing is truncated). Also, even if i output in text format, if it was very close to zero, i would get the value in exponential form, not truncated.
Faken
+1  A: 

Since a tag says visual-c++, it seems you're running on Windows on an Intel or compatible chip, so integer values of -0 aren't possible. Maybe you have a floating point value that's negative but very close to 0, for example -0.000000000000009, and maybe you're printing it with only a few digits of precision, for example -0.00000. In this case you could do something like:

if (x > -0.0000001 && x <= 0) x = 0;

Of course you want to do it with more style than that, but that gives you an idea.

The main text of your question doesn't say Visual C++ or Windows or Intel. If you're running on a one's complement machine, integer values of -0 are possible. Normally -0 compares equal to +0, so the following code would normalize integer zeros:

if (y == 0) y = 0;  // This looks redundant but it turns a -0 into a +0
Windows programmer
Umm...what do you mean -0 is not possible? its an IEEE standard...
Faken
There's an IEEE standard for integer values of -0?
Windows programmer
Yes, http://en.wikipedia.org/wiki/IEEE_754, IEEE standard for floating point math. It requires the definition of +0 and -0.
Faken
I don't think the IEEE standard for floating point math defines a standard for integer values of -0. Anyway you edited your question later to say that you're not using integers, so maybe it's better if you ignore the parts of my answer about integers and concentrate on the floats. (But if the IEEE floating standard really defines an integer as well, please kindly inform me.)
Windows programmer
Well, I'm at the limits of my knowledge on computer hardware. Anyways, your second bit of code there looks reasonable, ill give that a try see what happens, thanks!
Faken
Well, my second bit of code there was designed for the purpose of integers on one's complement machines, but coincidentally it will help you too because of the way you're putting floats together.
Windows programmer
Just so I can understand the code next year, please consider `if (y == -0.0) y = 0.0;`.
MSalters