https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127798
Bug ID: 127798
Summary: hypot builtin is incorrect on (DBL_SNAN, 0.0) for C
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: vincent-gcc at vinc17 dot net
Target Milestone: ---
In C, with the hypot builtin, hypot (DBL_SNAN, 0.0) gives a signaling NaN
instead of a quiet NaN. This can be tested with:
#include <stdio.h>
#include <float.h>
#include <math.h>
#include <inttypes.h>
union dbl
{
double d;
uint64_t q;
};
void out (double d)
{
union dbl x;
x.d = d;
printf ("%g (%016" PRIx64 ")\n", d, x.q);
}
int main (void)
{
double a = hypot (DBL_SNAN, 1.0);
double b = hypot (a, INFINITY);
double c = hypot (DBL_SNAN, 0.0);
double d = hypot (c, INFINITY);
out (DBL_SNAN);
out (a);
out (b);
out (c);
out (d);
return 0;
}
Using gcc (Debian 20260902-1) 17.0.0 20260902 (experimental) [trunk
r17-3841-g59b82e3f4f8] and
* "gcc hypot-snan.c -o hypot-snan -lm -fno-builtin":
nan (7ff4000000000000)
nan (7ffc000000000000)
inf (7ff0000000000000)
nan (7ffc000000000000)
inf (7ff0000000000000)
which is fine.
* "gcc hypot-snan.c -o hypot-snan -lm":
nan (7ff4000000000000)
nan (7ffc000000000000)
inf (7ff0000000000000)
nan (7ff4000000000000)
nan (7ffc000000000000)
The first 3 results are still fine, but hypot (DBL_SNAN, 0.0) returns a
signaling NaN (same encoding as in the first line), and as a consequence, the
last result is nan instead of inf.
According to https://en.cppreference.com/cpp/numeric/math/hypot ("if one of the
arguments is ±0, std::hypot(x, y) is equivalent to std::fabs called with the
non-zero argument."), it seems that the result would be correct in C++, though.