[PATCH] math: Fix errno setting for narrowing sqrt functions on underflow [BZ #33393]
Joseph Myers
josmyers@redhat.com
Mon Oct 20 20:33:02 GMT 2025
On Mon, 20 Oct 2025, Wilco Dijkstra wrote:
> Hi Osama,
>
> > Regarding the policy question: I understand glibc currently only sets errno
> > for underflow-to-zero. However, the bug report (BZ #33393) shows a case where:
> > - Input causes underflow during narrowing
> > - Result is subnormal (2.938736e-39)
> > - FE_UNDERFLOW is raised
> > - math_errhandling indicates errno should be set
> > - But errno remains 0
> >
> > Should the behavior be:
> > a) errno only for exact zero (current behavior)
> > b) errno for any underflow (zero or subnormal)
> >
> > If (a) is correct, should we close this bug as "won't fix"? Or is there a
> > standards-based reason to prefer (b)?
>
> GLIBC implements (a) for some math functions. For many others, it does
> (c) don't set errno. There are cases where you can get (b) due to rounding
> away from zero (but we tend not to care for such corner cases).
>
> I don't see a point in implementing (b) for just this function - that would
> add even more inconsistencies to GLIBC. Note that math_errhandling
> makes setting errno on underflow implementation defined, so it may actually
> be better to implement (c) and remove MATH_ERRNO from math_errhandling
> (since we're lying about setting errno for many math functions).
>
> So yes, I'd suggest to close it as "not-a-bug".
Indeed, it's not a bug and the proposed patch is incorrect.
You *can't* implement (b) correctly simply by checking the rounded result
anyway, since exact subnormal cases are not underflows (do not raise the
underflow flag under IEEE rules, and signaling the exception without
raising the flag is not detectable with default exception handling) and
it's thus improper to set errno for them either, while certain cases with
a rounded result with the least normal magnitude are underflows (tiny and
inexact), depending on the infinite-precision result, the rounding mode
and whether the architecture uses before-rounding or after-rounding
tininess detection (we take care about handling this when needed to raise
the right exception flags for strto* functions and software fma
implementations).
Setting of errno on underflow with math_errhandling & MATH_ERRNO is
implementation-defined in the standard. The glibc documentation of the
implementation-defined behavior is that it's only expected to be set for
underflow to zero (and might or might not be set for some cases that
underflow but not to zero).
--
Joseph S. Myers
josmyers@redhat.com
More information about the Libc-alpha
mailing list