commonfns: Fix max_error initialization and improve test output (#2500)

Before this change, `max_error` was initialized to `0.0f`, which caused
issues
when the actual maximum `error` was also `0.0f`. In such cases,
`max_val` would
never be updated, leading to misleading test output showing `NaN`
values:

```
  degrees: Max error 0.000000 ulps at 0: *nan vs 0x1.9802318e8abefp+21
```

This fix initializes `max_error` to `-INFINITY` to ensure `max_val` is
always
updated at least once. Additionally, the log output now includes the
input
value for better debugging:

```
  degrees: Max error 0.000000 ulps at 0, input 0x1.c7bffcp+15: *0x1.9802318e8abefp+21 vs 0x1.9802318e8abefp+21
```

This makes the test output more informative and eliminates confusing
`NaN` values in the summary.
This commit is contained in:
Yilong Guo
2025-10-14 23:47:59 +08:00
committed by GitHub
parent d733c2b802
commit 3871149208

View File

@@ -57,7 +57,7 @@ namespace {
template <typename T>
int verify_degrees(const T *const inptr, const T *const outptr, int n)
{
float error, max_error = 0.0f;
float error, max_error = -INFINITY;
double r, max_val = NAN;
int max_index = 0;
@@ -89,13 +89,16 @@ int verify_degrees(const T *const inptr, const T *const outptr, int n)
}
if (std::is_same<T, half>::value)
log_info("degrees: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
max_error, max_index, max_val, conv_to_flt(outptr[max_index]),
max_val, conv_to_flt(outptr[max_index]));
log_info("degrees: Max error %f ulps at %d, input %a: *%a vs %a (*%g "
"vs %g)\n",
max_error, max_index, conv_to_flt(inptr[max_index]), max_val,
conv_to_flt(outptr[max_index]), max_val,
conv_to_flt(outptr[max_index]));
else
log_info("degrees: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
max_error, max_index, max_val, outptr[max_index], max_val,
outptr[max_index]);
log_info("degrees: Max error %f ulps at %d, input %a: *%a vs %a (*%g "
"vs %g)\n",
max_error, max_index, conv_to_flt(inptr[max_index]), max_val,
outptr[max_index], max_val, outptr[max_index]);
return 0;
}
@@ -103,7 +106,7 @@ int verify_degrees(const T *const inptr, const T *const outptr, int n)
template <typename T>
int verify_radians(const T *const inptr, const T *const outptr, int n)
{
float error, max_error = 0.0f;
float error, max_error = -INFINITY;
double r, max_val = NAN;
int max_index = 0;
@@ -135,13 +138,16 @@ int verify_radians(const T *const inptr, const T *const outptr, int n)
}
if (std::is_same<T, half>::value)
log_info("radians: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
max_error, max_index, max_val, conv_to_flt(outptr[max_index]),
max_val, conv_to_flt(outptr[max_index]));
log_info("radians: Max error %f ulps at %d, input %a: *%a vs %a (*%g "
"vs %g)\n",
max_error, max_index, conv_to_flt(inptr[max_index]), max_val,
conv_to_flt(outptr[max_index]), max_val,
conv_to_flt(outptr[max_index]));
else
log_info("radians: Max error %f ulps at %d: *%a vs %a (*%g vs %g)\n",
max_error, max_index, max_val, outptr[max_index], max_val,
outptr[max_index]);
log_info("radians: Max error %f ulps at %d, input %a: *%a vs %a (*%g "
"vs %g)\n",
max_error, max_index, conv_to_flt(inptr[max_index]), max_val,
outptr[max_index], max_val, outptr[max_index]);
return 0;
}