X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f From: Kbwms AT aol DOT com Message-ID: <5f.43e4a8d9.2d356a0e@aol.com> Date: Tue, 13 Jan 2004 10:34:38 EST Subject: Re: C99 Functions Under Development and Checkout To: djgpp-workers AT delorie DOT com MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="part1_5f.43e4a8d9.2d356a0e_boundary" X-Mailer: 8.0 for Windows sub 6021 Reply-To: djgpp-workers AT delorie DOT com --part1_5f.43e4a8d9.2d356a0e_boundary Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: quoted-printable In reviewing my code for the long double functions, I've referred to specifications in both C99 [ISO/IEC 9899:1999 (E)] and Posix, at http://www.opengroup.org/onlinepubs/007904975/. There are wide discrepancies in these documents for the llrint functions. Paragraph 7.12.9.5 of ISO/IEC 9899:1999 (E) states: > > The lrint and llrint functions round their argument to the > nearest integer value, rounding according to the current > rounding direction. If the rounded value is outside the > range of the return type, the numeric result is unspecified. > A range error may occur if the magnitude of x is too large. >=20 Further, in paragraph F.9.6.5 is the following: >=20 > The lrint and llrint functions provide floating-to-integer=20 > conversion as prescribed by IEC 60559. They round according=20 > to the current rounding direction. If the rounded value is=20 > outside the range of the return type, the numeric result is=20 > unspecified and the invalid exception is raised. When they=20 > raise no other exception and the result differs from the=20 > argument, they raise the inexact exception. In the Posix document for llrint, llrintf, and llrintl are the=20 following specifications: > These functions shall round their argument to the nearest=20 > integer value, rounding according to the current rounding=20 > direction. >=20 > An application wishing to check for error situations should=20 > set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before=20 > calling these functions. On return, if errno is non-zero or=20 > fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW)=20 > is non-zero, an error has occurred. >=20 > RETURN VALUE > Upon successful completion, these functions shall return the=20 > rounded integer value. >=20 > If x is NaN, a domain error shall occur, and an unspecified value is=20 returned. > If x is +Inf, a domain error shall occur and an unspecified value is=20 returned. > If x is -Inf, a domain error shall occur and an unspecified value is=20 returned. > If the correct value is positive and too large to represent as a long long= ,=20 > a domain error shall occur and an unspecified value is returned. >=20 > If the correct value is negative and too large to represent as a long long= ,=20 > a domain error shall occur and an unspecified value is returned.=20 >=20 > ERRORS > These functions shall fail if: >=20 > Domain Error=20 > The x argument is NaN or =B1Inf, or the correct value is not representable= =20 > as an integer. If the integer expression (math_errhandling & MATH_ERRNO)=20 > is non-zero, then errno shall be set to [EDOM]. If the integer expression=20 > (math_errhandling & MATH_ERREXCEPT) is non-zero, the invalid floating-poin= t=20 > exception shall be raised.=20 >=20 So, C99 says return a range error if the rounded result is too large, whereas Posix requires a domain error regardless of the kind of error. Which way is the right way? The code, written a couple of years ago,=20 proceeds as follows: If the argument is a NaN, set domain error [EDOM]. If the rounded=20 result is too large (including +- Inf), set range error [ERANGE]. In both cases, the invalid floating-point exception is raised. KB Williams --part1_5f.43e4a8d9.2d356a0e_boundary Content-Type: text/html; charset="ISO-8859-1" Content-Transfer-Encoding: quoted-printable In reviewing my code for the long dou= ble functions, I've referred
to specifications in both C99 [ISO/IEC 9899:1999 (E)] and Posix,
at http://www.ope= ngroup.org/onlinepubs/007904975/.  There are
wide discrepancies in these documents for the llrint functions.

Paragraph 7.12.9.5 of ISO/IEC 9899:1999 (E) states:

&= gt;
> The lrint and llrint functions round their argument to the
> nearest integer value, rounding according to the current
> rounding direction. If the rounded value is outside the
> range of the return type, the numeric result is unspecified.
> A range error may occur if the magnitude of x is too large.
>

Further, in paragraph F.9.6.5 is the following:

>
> The lrint and llrint functions provide floating-to-integer
> conversion as prescribed by IEC 60559. They round according
> to the current rounding direction. If the rounded value is
> outside the range of the return type, the numeric result is
> unspecified and the invalid exception is raised. When they
> raise no other exception and the result differs from the
> argument, they raise the inexact exception.
<= BR>
In the Posix document for llrint, llrintf, and llrintl are the
following specifications:

&= gt; These functions shall round their argument to the nearest
> integer value, rounding according to the current rounding
> direction.
>
> An application wishing to check for error situations should
> set errno to zero and call feclearexcept(FE_ALL_EXCEPT) before
> calling these functions. On return, if errno is non-zero or
> fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW) > is non-zero, an error has occurred.
>
> RETURN VALUE
> Upon successful completion, these functions shall return the
> rounded integer value.
>
> If x is NaN, a domain error shall occur, and an unspecified value is re= turned.
> If x is +Inf, a domain error shall occur and an unspecified value is re= turned.
> If x is -Inf, a domain error shall occur and an unspecified value is re= turned.
> If the correct value is positive and too large to represent as a long l= ong,
> a domain error shall occur and an unspecified value is returned.
>
> If the correct value is negative and too large to represent as a long l= ong,
> a domain error shall occur and an unspecified value is returned.
>
> ERRORS
> These functions shall fail if:
>
> Domain Error
> The x argument is NaN or =B1Inf, or the correct value is not representa= ble
> as an integer. If the integer expression (math_errhandling & MATH_E= RRNO)
> is non-zero, then errno shall be set to [EDOM]. If the integer expressi= on
> (math_errhandling & MATH_ERREXCEPT) is non-zero, the invalid floati= ng-point
> exception shall be raised.
>
<= BR> So, C99 says return a range error if the rounded result is too large,
whereas Posix requires a domain error regardless of the kind of error.

Which way is the right way?  The code, written a couple of years ago, <= BR> proceeds as follows:

If the argument is a NaN, set domain error [EDOM].  If the rounded
result is too large (including +- Inf), set range error [ERANGE].
In both cases, the invalid floating-point exception is raised.


KB Williams
--part1_5f.43e4a8d9.2d356a0e_boundary--