[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] Overflow to Infinity
From: |
Andrew J. Schorr |
Subject: |
Re: [bug-gawk] Overflow to Infinity |
Date: |
Fri, 6 Jul 2018 05:35:03 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On Fri, Jul 06, 2018 at 11:46:36AM +0300, Eli Zaretskii wrote:
> > +static char *
> > +format_nan_inf(double val)
> > +{
> > + static char buf[100];
> > +
> > + if (isnan(val)) {
> > + sprintf(buf, "%g", val);
> > + if (strcmp(buf, "nan") == 0) // "-nan" or "nan"
> > + strcpy(buf, "+nan");
>
> This is non-portable. On MS-Windows, the string you may get is
> "1.#IND", not "nan", depending on the version of the C runtime. It
> can also have a leading sign, as in "-1.#IND".
>
> Why do you need to sprintf the value, if it is known that it's a NaN?
> Why not do what you do with Inf:
>
> > + } else if (isinf(val)) {
> > + strcpy(buf, val < 0 ? "-inf" : "+inf");
I don't think you get a different result when comparing +nan or -nan to 0,
do you? Is there a way to test for NaN signedness?
Perhaps this would work:
+ if (isnan(val)) {
+ sprintf(buf, "%g", val);
+ strcpy(buf, buf[0] == '-' ? "-nan" : "+nan");
But it seems like there ought to be a math library function to do this.
After all, sprintf figures out the sign somehow. Does the 'signbit'
function work with NaN values? I guess one needs to dig into the glibc
sprintf code, but I don't have time now.
Regards,
Andy