[PATCH] math: Fix errno setting for narrowing sqrt functions on underflow [BZ #33393]
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Sun Oct 19 18:22:38 GMT 2025
Hi Osama,
> The CHECK_NARROW_SQRT macro in math-narrow.h was not setting errno to
> ERANGE when an underflow exception occurred during narrowing square root
> operations. While the FE_UNDERFLOW exception flag was correctly set, errno
> remained unchanged, violating IEEE 754 and POSIX requirements.
I don't see an issue that needs to be fixed. errno as a concept is highly problematic
to start with. It's not very useful for math functions, hence most math libraries do
not set errno at all - and this is perfectly conformant. Note IEEE-754 does not
mandate anything about errno.
GLIBC typically sets errno to ERANGE on underflow to zero [1]. And some
GLIBC functions never set errno.
else if ((RET) == 0 && (X) != 0) \
__set_errno (ERANGE); \
+ else if (fetestexcept (FE_UNDERFLOW)) \
+ __set_errno (ERANGE); \
This is incorrect since FE_UNDERFLOW may be set by a different math operation.
Cheers,
Wilco
[1] https://sourceware.org/glibc/manual/2.42/html_node/Math-Error-Reporting.html
More information about the Libc-alpha
mailing list