From: "H.Yamamoto" Date: 2004-07-03T11:39:52+09:00 Subject: Re: bug in String#to_f in 1.9 (was Re: ruby-dev summary 23690-23762) Hello. I think this patch works... Paul Brannan wrote: (2004/07/03 02:45) >(Also, it looks to me like in rb_cstr_to_dbl, errno is being checked >incorrectly; errno is only set if +/- HUGE_VAL is returned, otherwise it >is left to the value it was set before strtod was called. The fix >should be to zero out errno before calling strtod). This is OK, because errno is set to zero in ruby_strtod which is used as strtod by #define. Index: numeric.c =================================================================== RCS file: /var/cvs/src/ruby/numeric.c,v retrieving revision 1.112 diff -u -w -b -p -r1.112 numeric.c --- numeric.c 16 Jun 2004 14:21:33 -0000 1.112 +++ numeric.c 3 Jul 2004 02:07:37 -0000 @@ -495,7 +495,7 @@ flo_to_s(flt) char *p, *e; if (isinf(value)) - return rb_str_new2(value < 0 ? "-Infinity" : "Infinity"); + return rb_str_new2(value < 0 ? "-Inf" : "Inf"); else if(isnan(value)) return rb_str_new2("NaN"); Index: util.c =================================================================== RCS file: /var/cvs/src/ruby/util.c,v retrieving revision 1.43 diff -u -w -b -p -r1.43 util.c --- util.c 14 May 2004 03:17:29 -0000 1.43 +++ util.c 3 Jul 2004 02:11:15 -0000 @@ -750,6 +750,7 @@ ruby_strtod(string, endPtr) * in string. */ const char *pExp; /* Temporarily holds location of exponent * in string. */ + int frac1, frac2; /* * Strip off leading blanks and check for a sign. @@ -772,6 +773,22 @@ ruby_strtod(string, endPtr) } /* + * Check for NaN and Inf. + */ + + if (strncmpi(p, "NaN", 3) == 0) { + p += 3; + fraction = 0.0 / 0; + goto exit; + } + + if (strncmpi(p, "Inf", 3) == 0) { + p += 3; + fraction = 1.0 / 0; + goto exit; + } + + /* * Count the number of digits in the mantissa * and also locate the decimal point. */ @@ -812,8 +829,7 @@ ruby_strtod(string, endPtr) fracExp += (mantSize - 18); mantSize = 18; } - { - int frac1, frac2; + frac1 = 0; for ( ; mantSize > 9; mantSize -= 1) { c = *p; @@ -920,12 +936,11 @@ ruby_strtod(string, endPtr) else { fraction += frac2 * dblExp; } - } + exit: if (endPtr != NULL) { *endPtr = (char *) p; } - if (sign) { return -fraction; } Index: util.h =================================================================== RCS file: /var/cvs/src/ruby/util.h,v retrieving revision 1.14 diff -u -w -b -p -r1.14 util.h --- util.h 4 Aug 2003 01:27:25 -0000 1.14 +++ util.h 3 Jul 2004 02:11:20 -0000 @@ -62,5 +62,6 @@ char *ruby_getcwd _((void)); double ruby_strtod _((const char*, char **)); #define strtod(s,e) ruby_strtod(s,e) +#define atof(s) ruby_strtod(s,0) #endif /* UTIL_H */