Negative zero and IEEE floating point (again).

uunet!netcom.com!segfault!rfg uunet!netcom.com!segfault!rfg
Sat Mar 27 17:17:02 PST 1993


OK.  One reader already pointed out to me the error of my ways.  The small
IEEE test I posted earlier did indeed have a minor bug, and this was
definitely responsible for a number of the "failing" results I was getting
for a number of compilers.

The problem was that I had written -(0) where I should have written -(0.0)
and I wrote -(1-1) where I should have written -(1.0-1.0).

A corrected version of this small test file is provided below for those
who are interested.  (If you do try compiling and running this test, you
may wish to do so both with and without optimization enabled, as that may
influence the results.)

Separately, another reader brought my attention to the fact that the NCEG's
"Floating-point C Extensions" document does already provide a "binding"
of C to IEEE floating-point when it comes to the *printf functions.  (I
had not noticed this before.)  I am very glad to see this binding specifi-
cation, even if it is in a document which is ostensibly about "extensions"
to ANSI C.  I do have one question about this stuff though.  I see in
section 4.2.1.2 where it says (in two different places) that strtod must
respect the sign of zero.  Why aren't these statements generalized to
include the strtof and strtold functions also?

// rfg


cut here
============================================================================
/* (C) 1993 Ronald F. Guilmette; all rights reserved.  */

/* Check that the signedness of zero is respected.

   This test is appropriate only for target systems claiming to support
   the IEEE 754 or 854 floating-point standards.
*/

#include <stdio.h>

int err_count = 0;

double zero = 0.0;
double one = 1.0;
double some_infinity;

double d;

int
test ()
{
  d = -(0.0);
  some_infinity = 1.0/d;
  if (some_infinity > 0)	/* CHECK */
    {
      printf ("negating (0.0) doesn't produce -0\n");
      err_count++;
    }

  d = -(zero);
  some_infinity = 1.0/d;
  if (some_infinity > 0)	/* CHECK */
    {
      printf ("negating (zero) doesn't produce -0\n");
      err_count++;
    }

  d = -(1.0-1.0);
  some_infinity = 1.0/d;
  if (some_infinity > 0)	/* CHECK */
    {
      printf ("negating (1.0-1.0) doesn't produce -0\n");
      err_count++;
    }

  d = -(one-one);
  some_infinity = 1.0/d;
  if (some_infinity > 0)	/* CHECK */
    {
      printf ("negating (one-one) doesn't produce -0\n");
      err_count++;
    }

  return err_count;
}

int main () { return test (); }



More information about the Numeric-interest mailing list