From: Paul Brannan Date: 2004-07-03T02:45:41+09:00 Subject: bug in String#to_f in 1.9 (was Re: ruby-dev summary 23690-23762) On Sat, Jun 26, 2004 at 08:38:51AM +0900, Masayoshi Takahashi wrote: > Nakamura pointed out that the string "nan" and "inf" are > used as NaN and Inf in some environments. His patch > forced to use "NaN" and "Inf". He needs comments > about this change. I think it's a good idea to print NaN and Inf uniformly across all platforms. However, a thought occurred to me when I was considering this, that it would be a shame if we print NaN to a string and then were unable to read it back in, because to_f didn't handle the case change correctly. It should work though, since #to_f is implemented in terms of strtod, and strtod can handle "NAN" in any case. However, it seems this doesn't work in the first place in 1.8 nor 1.9: irb(main):001:0> (0.0/0).to_s.to_f => 0.0 irb(main):002:0> RUBY_VERSION => "1.9.0" irb(main):004:0> RUBY_RELEASE_DATE => "2004-06-22" But it does work on 1.6.8: [pbrannan@zaphod images]$ irb irb(main):001:0> (0.0/0).to_s.to_f => NaN irb(main):002:0> "NAN".to_f => NaN irb(main):003:0> "nan".to_f => NaN irb(main):004:0> RUBY_VERSION => "1.6.8" (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). Paul