Document: WG14 N1466


fabs


Submitter: Fred J. Tydeman (USA)
Submission Date: 2010-05-10
Related documents: N1429
Subject: fabs

Background: IEC 60559-1989 [IEEE-754-1985] has the following:

Some functions, such as the copy operation y := x without change of format, may at the implementor's option be treated as nonarithmetic operations which do not signal the invalid operation exception for signaling NaNs; the functions in question are (1), (2), (6), and (7).

1. Copysign(x, y) returns x with the sign of y. Hence, abs(x) = copysign(x, 1.0), even if x is NaN.

Since Annex F of C99 is a binding between C language and IEC 60559, the requirements of IEC 60559 apply. But, C99 did not do a good job of specifying some of those requirements.

C99 in F.10 Mathematics <math.h>, paragraph 11 has:

Functions with a NaN argument return a NaN result and raise no floating-point exception, except where stated otherwise.

C99 in F.10 Mathematics <math.h>, paragraph 13 has:

Recommended practice
If a function with one or more NaN arguments returns a NaN result, the result should be the same as one of the NaN arguments (after possible type conversion), except perhaps for the sign.

Hence, currently, C99 allows fabs(NaN) to:

  1. Return a different NaN
  2. Leave the sign bit set (negative)

I believe that the current C99 behaviour allowed conflicts with IEC 60559.

Add to F.9.4.2 the fabs functions a new bullet:

-- fabs(NaN) returns the same NaN with its sign bit set to 0 (positive).

Add to Raionale:

In IEC 60559, fabs() is considered a bit operation that affects only the sign bit.