Document: WG14 N1398


Treatment of math error conditions


Submitter: Fred J. Tydeman (USA)
Submission Date: 2009-09-18
Related WG14 documents: N1354, DR 330
Subject: Treatment of math error conditions

Currently, some members of the committee believe that errno is undefined, indeterminate, or unspecified after calling a math function, even if no error happens. This makes it difficult for application writters to write robust mathematical software that uses the math library functions.

In addition, POSIX assumes that errno will only be set to non-zero by a math function if an error happens, and that errno will be left as 0 if no error happens.

POSIX XBD: 4.19 Treatment of Error Conditions for Mathematical Functions has:

For all the functions in the <math.h> header, an application wishing to check for error situations should set errno to 0 and call feclearexcept(FE_ALL_EXCEPT) before calling the function. On return, if errno is non-zero or fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) is non-zero, an error has occurred.

Defect Report 330 changed 7.12.1 paragrpah 1 last sentence to:

Each function shall execute as if it were a single operation without generating any of the exceptions "invalid", "divide-by-zero", or "overflow" except to reflect the result of the function.

Does "single operation" mean that the math functions cannot do "raise(SIGFPE)"? If so, should we make that requirement explicit?

If math errors are being indicated by errno, what happens to the floating-point status flags?

If math errors are being indicated by the floating-point status flags, what happens to errno?

I propose the following (DO A SEPERATE VOTE ON EACH CHANGE).

In 7.12.1, Treatment of error conditions, 1st paragraph: Add:

  1. No math function shall do the equivalent of signal(SIGFPE).
  2. If no error occurs, errno shall be unaltered.
  3. If no error occurs, none of "invalid", "div-by-zero", "overflow" shall be raised.
  4. If an error occurs and if the integer expression (math_errhandling & MATH_ERRNO) is zero [footnote], either errno is unaltered or errno acquires the value corresponding to the error condition.

    [Footnote] Math errors are being indicated by the floating-point exception flags.

  5. If an error occurs and if the integer expression (math_errhandling & MATH_ERREXCEPT) is zero [footnote], either none of the exceptions "invalid", "divide-by-zero", or "overflow" is raised or the floating-point exception corresponding to the error condition is raised.

    [Footnote] Math errors are being indicated by errno.