On Dec 22, 2:20 am, blargg....@gishpuppy.com (blargg) wrote:
> James Kanze wrote:
> > On Dec 21, 2:52 am, blargg....@gishpuppy.com (blargg) wrote:
> [...]
> > > Will <0 catch negative zero?
> > Of course not.
> > > I'd use <= -0.0 above.
> > Which is the same as <= 0.0.
> > > Remember, FP is usually not two's complement, so it has
> > > negative zero.
> > Which is irrelevant in most cases. Positive and negative 0
> > are required to compare equal, not less than or greater
> > than. They're just artifacts of the representation: a
> > positive 0 isn't strictly positive, and a negative 0 isn't
> > strictly negative---both are zero.
> So it seems. Maybe it's just my implementation, but printf
> does put a negative sign before a negative zero (cout
> doesn't).
I would consider that a very serious bug. It seems to be
universal, however:
int main()
{
double d = 0.0 ;
d *= -1.0 ;
printf( "%f\n", d ) ;
std::cout << std::fixed << d << std::endl ;
return 0 ;
}
Outputs -0.000000 twice with all compilers/libraries I have
access too. That's three different librarys for printf, and
four for std::cout. And it leads to the interesting point that
two values can compare equal, but not result in the same output.
(Note that if the 0.0 is a result of rounding, there might be
some justification. If, for example, I was outputting something
like -1E20 with "%f" or std::fixed. Even then, however, I doubt
it.)
[...]
> Assuming the goal is to parenthesize values with a negative
> sign, how would one do the conditional in print()? As you
> said, negative and positive zero both compare <= -0.0.
Portably, of course, you can't, since there's no guarantee that
you have a negative zero

. But the C99 standard provides the
macro "signbit()", with a footnote specifying that "The signbit
macro reports the sign of all values, including infinities,
zeros, and NaNs. If zero is unsigned, it is treated as
positive." So if your implementation supports C99 (and this
will be in C++0x), you can use that.
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34