[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

math error reporting

From: Bruno Haible
Subject: math error reporting
Date: Sun, 6 Nov 2011 21:36:40 +0100
User-agent: KMail/1.13.6 (Linux/; KDE/4.6.0; x86_64; ; )


Considering how gnulib-implemented <math.h> functions should do their
error reporting, I ran a test program to see how the various systems
implement math errors.

#include <errno.h>
#include <math.h>
#include <stdio.h>
#ifndef NO_FENV
#include <fenv.h>
int main()
  double result;
  errno = 0;
#ifndef NO_FENV
  fprintf (stderr, "before: %s\n",
           fetestexcept (FE_INVALID) ? "invalid" : "");
  result = sqrt (-1.0);
#ifndef NO_FENV
  fprintf (stderr, "%g, after: %s, errno=%d\n", result,
           fetestexcept (FE_INVALID) ? "invalid" : "", errno);
  fprintf (stderr, "%g, after: errno=%d\n", result, errno);
  return 0;

The result:

   glibc 2.11:   NaN, fenv bit, errno
   MacOS X 10.5: NaN, fenv bit          math_errhandling (always=MATH_ERREXCEPT)
   FreeBSD 6.4:  NaN, fenv bit          math_errhandling (always=MATH_ERREXCEPT)
   OpenBSD 4.9:  NaN
   NetBSD 5.1:   NaN,           errno
   AIX 7.1:      NaN, fenv bit, errno   math_errhandling (always=MATH_ERRNO)
   HP-UX 11.31:  NaN, fenv bit, errno
   IRIX 6.5:     NaN
   OSF/1 5.1:    0,             errno   (<fenv.h> incomplete)
   Solaris 10:   NaN, fenv bit, errno   math_errhandling (always=MATH_ERREXCEPT)
   Cygwin 1.7.9: NaN,           errno
   mingw:        NaN, fenv bit, errno
   MSVC 9:       NaN,           errno

* All platforms except OSF/1 return a NaN value.

* All platform that have a complete <fenv.h> (OSF/1 has an incomplete
  <fenv.h>) set the FE_INVALID bit in sqrt(-1).

* Some platforms set errno, some don't.

* math_errhandling is missing on many platforms. On those platforms where
  it is defined, it is useless, though:
    - All platforms that have math_errhandling also have <fenv.h>, therefore
      you don't need to consider math_errhandling if you want to know whether
      to set a floating-point exception bit.
    - Some of the platforms that set errno have a math_errhandling that
      includes MATH_ERRNO (AIX), some don't (Solaris).

I am inclined to do the following in gnulib:

1) Provide a complete <fenv.h> on all platforms. glibc contains the code
   for the CPUs which are missing in the table above (x86, x86_64, sparc,
   mips, alpha).

2) Define math_errhandling everywhere, and override it on AIX and Solaris.

How does that sound? (It's a lot of work, I know.)

Is it likely that the IRIX, OSF/1, or MSVC compilers are implementing the
math operators and functions in a way does not set the floating-point
exception bits correctly (thus making <fenv.h> useless)?

In memoriam Louis Philippe d'Orléans 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]