fmod(), remainder(), and remquo() for a zero divisorThis issue has been automatically converted from the original issue lists and some formatting may not have been preserved.
Authors: Fred Tydeman (US)
Date: 2001-02-25
Reference document: ISO/IEC WG14 N943
Submitted against: C99
Status: Fixed
Fixed in: C99 TC2
Converted from: summary-c99.htm, dr_243.htm
fmod(),remainder(), andremquo()should be made consistent with each other when the divisor is zero. There are two "correct" behaviours when the divisor is zero: Treat it as a domain error (cannot divide by zero), or, based upon limits, compute a zero remainder. This series of changes may also require that IEEE-754 be changed (to allow a zero result in addition to the currently required invalid operation). Assuming thatfmod(),remainder(), andremquo()should be consistent with each other, the following needs to be done.An alternative is to do some of these changes, or changes along these lines.
It is assumed that requiring just the return of zero behaviour is too drastic as there are many millions of chips already doing the invalid operation behaviour.
For a fixed
x, as one takes the limit asyapproaches zero, the remainder ofx/yapproaches zero(0 <= |result| < |y|)and the quotient is unspecified.IEC 60559 requires that
xREMy, whenyis zero, be an invalid operation, e.g., a domain error.
fmod(x,0)is currently allowed to be either 0 or a domain error by 7.12.10.1, while Annex F requires it to be an invalid exception, e.g., domain error.
remainder(x,0)is currently unspecified by 7.12.10.2, while Annex F requires it to be an invalid exception, e.g., domain error.
remquo(x,0)is currently unspecified by 7.12.10.3, while Annex F requires it to be an invalid exception, e.g., domain error. In addition, nothing is said about the quotient that is stored for this case.Counter-argument: These functions are discontinuous along the lines
y = mxory = (m+1/2)xfor integers m. We see no reason to "take the limit asyapproaches zero".Allowing two different behaviours for these functions for the same arguments, will cause applications to be more complicated, with no real added benefit.
Counter-counter-argument: By discontinuous, I assume you mean that they are like saw-tooth shaped functions, e.g., a linear rise and a vertical fall. I agree with that, but, as one approaches the line
y=0, the height of the teeth gets smaller and smaller.
In 7.12.10.1 fmod:
No change needed.
In 7.12.10.2 remainder:
Add to Returns:
If
yis zero, whether a domain error occurs or theremainderfunctions return zero is implementation defined.
In 7.12.10.3 remquo:
Add to Returns:
If
yis zero, whether a domain error occurs or theremquofunctions return zero is implementation defined.If
yis zero, the quotient stored is unspecified.
In F.9.7.1 fmod:
Change
fmod(x,y)returns aNaNand raises the "invalid" floating-point exception forxinfinite oryzero.
to two items:
fmod(x,y)returns aNaNand raises the "invalid" floating-point exception forxinfinite.
and
For
yzero,fmod(x,y)either returns a zero (with sign ofx), or returns aNaNand raises the "invalid" floating-point exception.
In F.9.7.2 remainder:
Add:
For
yzero,remainder(x,y)either returns a zero (with sign ofx), or returns aNaNand raises the "invalid" floating-point exception.
In F.9.7.3 remquo:
Add:
For
yzero,remquo(x,y)either returns a zero (with sign ofx), or returns aNaNand raises the "invalid" floating-point exception; and, in both cases, has an unspecified quotient stored.
Also add,
When
remquoreturns aNaN, the quotient stored is unspecified.
Comment from WG14 on 2001-10-16:
In 7.12.10.2 remainder:
Add to Returns:
If
yis zero, whether a domain error occurs or theremainderfunctions return zero is implementation defined.
In 7.12.10.3 remquo:
Add to Returns:
If
yis zero, whether a domain error occurs or theremquofunctions return zero is implementation defined.If
yis zero, the quotient stored is unspecified.
In J.3.12 Library functions:
Add (after fmod):
Whether a domain error occurs or zero is returned when an
remainderfunction has a second argument of zero (7.12.10.2).Whether a domain error occurs or zero is returned when an
remquofunction has a second argument of zero (7.12.10.3).