Markus
10/12/2004 4:15:00 PM
Yes, but his point is that "NaN" _is_ the string representation of
valid floating point value, defined and handled by the system, with
standardized semantics, etc.
"NaN".to_f should return NaN. How else are you going to reliably
get NaN if you need it?
IMO, this is a bug.
-- Markus
On Tue, 2004-10-12 at 02:39, Stephan Kämper wrote:
> Hi Thomas, hi Rubyists
>
> Thomas Fini Hansen wrote:
>
> > ---
> > irb(main):001:0> VERSION
> > "1.6.7"
> > irb(main):002:0> "nan".to_f
> > NaN
> > ---
> > irb(main):001:0> VERSION
> > => "1.8.2"
> > irb(main):002:0> "nan".to_f
> > => 0.0
> > ---
> >
> > (1.8.2 is really 1.8.1+1.8.2pre2-1woody1 in Debian)
> >
> > I wouldn't have been surprised if it was the other way round. Can
> > anyone explain why it is so? Seemed like a nice feature to me.
> >
>
> >ri String#to_f
> ------------------------------------------------------------ String#to_f
> str.to_f => float
> ------------------------------------------------------------------------
> Returns the result of interpreting leading characters in _str_ as a
> floating point number. Extraneous characters past the end of a
> valid number are ignored. If there is not a valid number at the
> start of _str_, +0.0+ is returned. This method never raises an
> exception.
>
> "123.45e1".to_f #=> 1234.5
>
> "45.67 degrees".to_f #=> 45.67
>
> "thx1138".to_f #=> 0.0
>
> I don't use String#to_f a lot, because I'd prefer Float( a_string ).
> Float() is stricter and raises an ArgumentError if the whole String
> can't be interpreted as a Float, while String#to_s tries to create a
> Float from the first characters and stops when/if there are any chars
> that wouldn't yield a Float.
>
> Happy rubying
>
> Stephan