Re: nextafter underflow and errno in Glibc

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

 



On Fri, 2023-03-10 at 07:34 +0000, Pascal Cuoq via Libc-alpha wrote:
> Hello,
> 
> I do not feel confident sending a patch for the following reason:
> 
> Glibc commit 85422c2acba83852396c9d9fd22ff0493e3606fe does contain a
> partial description of the specification that Glibc strives to
> implement:
> 
>     This patch makes them do so in cases that should include all the cases
>     where such errno setting is required by glibc's goals for when to set
>     errno (but not all cases of underflow where the result is nonzero and
>     so glibc's goals do not require errno setting).
> 
> However much remains unclear (for me!) about the specification that
> Glibc strives to implement. Does this mean that the specification is
> actually non-deterministic and that the manpage should contain a
> sentence saying “when the result of nextafter applied to distinct
> arguments x and y is subnormal, nextafter reserves the right to set
> errno to ERANGE but offers not guarantees of doing so; if the result
> is zero, errno is guaranteed to be set to ERANGE”?

> This does not make sense to me: how does one choose one's own
> specification for nextafter, over which one has full control, for a
> basic function that does not need to allocate resources, and make this
> specification non-deterministic? Does the non-determinism apply to the
> floating-point underflow exception as well?

The C17 standard says:

The result underflows if the magnitude of the mathematical result is so
small that the mathematical result cannot be represented, without
extraordinary roundoff error, in an object of the specified type. If the
result underflows, the function returns an implementation-defined value
whose magnitude is no greater than the smallest normalized positive
number in the specified type; if the integer expression math_errhandling
& MATH_ERRNO is nonzero, whether errno acquires the value ERANGE is
implementation-defined; if the integer expression math_errhandling &
MATH_ERREXCEPT is nonzero, whether the "underflow" floating-point
exception is raised is implementation-defined.

"Implementation-defined" means the implementation (Glibc here) has the
freedom to decide what to do, but it has to be documented.  It's
documented in Glibc manual section 20.5.4:

When underflow occurs, the underflow exception is raised, and zero
(appropriately signed) or a subnormal value, as appropriate for the
mathematical result of the function and the rounding mode, is returned.
‘errno’ may be set to ‘ERANGE’, but this is not guaranteed; it is
intended that the GNU C Library should set it when the underflow is to
an appropriately signed zero, but not necessarily for other underflows.

> But then again, I notice that the current manpage description is
> already non-deterministic: a result that should be subnormal can be
> 0.0, confirming that I do not understand Glibc's goals. In these
> conditions I could not do a good job describing the specification that
> Glibc implements.

Just copy from Glibc manual.  But IMO man-page is not a Glibc manual (if
you need Glibc manual you can just read Glibc manual), so a vague "may"
clause like "the errno MAY be set to ERANGE" should be enough (and it's
always true regardless which libc implementation is used). 

-- 
Xi Ruoyao <xry111@xxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux