[Cfp-interest 2337] Re: nan("1111111111111111111111....")

Vincent Lefevre vincent at vinc17.net
Sun Jan 16 18:07:04 PST 2022


On 2022-01-16 10:30:49 -0800, Fred J. Tydeman wrote:
> However, I have found that (at least) glibc raises FP_INVALID if the
> string passed to nan() has lots of valid characters. Is that a bug
> in glibc?

Not necessarily. AFAIK, it is accepted that library functions may
generate arbitrary exceptions in their implementation. But to be
sure that this is not a bug, one would need to find the cause.
In the present case, it seems that a part of the code is in
stdlib/strtod_nan_main.c, which has

  FLOAT retval = NAN;
[...]
  mant = STRTOULL (str, &endp, 0);
  if (endp == cp)
    SET_NAN_PAYLOAD (retval, mant);

and sysdeps/generic/math-nan-payload-double.h contains

#define SET_NAN_PAYLOAD(flt, mant)                      \
  do                                                    \
    {                                                   \
      union ieee754_double u;                           \
      u.d = (flt);                                      \
      u.ieee_nan.mantissa0 = (mant) >> 32;              \
      u.ieee_nan.mantissa1 = (mant);                    \
      if ((u.ieee.mantissa0 | u.ieee.mantissa1) != 0)   \
        (flt) = u.d;                                    \
    }                                                   \
  while (0)

where ieee_nan has a quiet bit separate from the other bits of the
mantissa field (i.e. the payload). So the code seems correct (it
will not generate a signaling NaN by setting the payload).

If the FP_INVALID comes from an overflow in STRTOULL, this would
explain the exception, but that would be surprising since I expect
STRTOULL to use integer operations only.

It is also possible that there is a particular code for the
processor.

Finding which integers yield a FP_INVALID could give some idea...
If this occurs only for i >= 2^64, that's probably STRTOULL.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the Cfp-interest mailing list