"NaN".to_f revisited

Hi all,

I thought ".to_s.to_f" should be an identity operation on any floating
point number, apart from a potential truncation error.

On Ruby 1.8.7,

   a = 0.0/0.0
   a.to_s # => "NaN"
   "NaN".to_f #=> 0.0

Therefore,

  a.to_s.to_f #=> 0.0,

which isn't acceptable to me. Consider writing floating point numbers
to a text file and reading them back in.

Is this a bug in 1.8.x? I searched Google but couldn't get a
definitive answer.

Regards,
Ryo

Hi all,

I thought ".to_s.to_f" should be an identity operation on any floating
point number, apart from a potential truncation error.

On Ruby 1.8.7,

  a = 0.0/0.0
  a.to_s # => "NaN"
  "NaN".to_f #=> 0.0

Therefore,

a.to_s.to_f #=> 0.0,

which isn't acceptable to me. Consider writing floating point numbers
to a text file and reading them back in.

Is this a bug in 1.8.x? I searched Google but couldn't get a
definitive answer.

Regards,
Ryo

If you want to round-trip to a file, use something like Marshal or YAML

a = 0.0/0.0

Marshal.dump(a)

=> "\x04\bf\bnan"

b = Marshal.load(Marshal.dump(a))
a.nan? #=> true
b.nan? #=> true

require 'yaml'

=> true

a.to_yaml

=> "--- .NaN\n"

c=YAML.load(a.to_yaml)

=> NaN

c.nan?

=> true

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

···

On Aug 10, 2010, at 8:30 PM, Ryo wrote:

Hi,

I thought ".to_s.to_f" should be an identity operation on any floating
point number, apart from a potential truncation error.

That would be a good property. The reason behind String#to_f not
supporting NaN and Inf is that I couldn't give up non IEEE754 floating
point architecture such as VAX at the time I implemented.

But we are no longer see any non-IEEE754 architecture around, and
current implementation of Ruby would require IEEE754 anyway, so it
might be a good chance to introduce roundtrip nature.

              matz.

···

In message "Re: "NaN".to_f revisited" on Wed, 11 Aug 2010 09:30:14 +0900, Ryo <furue@hawaii.edu> writes:

Might give some strange behaviour though:

"1$".to_f => 1.0
"nonsense".to_f => 0.0
"nanotubes".to_f => Nan ?
"inferiority".to_f => Inf ?

···

--
Posted via http://www.ruby-forum.com/.

Normally if the string starts with non-numeric characters, converting
to numbers yields a zero result. This is to remain consistent with
atof and strtod from the standard c library. On my machine, the man
page for strtod contains this paragraph:

     Alternatively, if the portion of the string following the optional plus
     or minus sign begins with ``INFINITY'' or ``NAN'', ignoring case, it is
     interpreted as an infinity or a quiet NaN, respectively.

In practice, strtod seems to actually recognize INF rather than the
fully-spelled-out INFINITY as the trigger for returning infinity. So,
your last 2 examples would return NaN and Inf if passed to strtod.

···

On 8/11/10, Brian Candler <b.candler@pobox.com> wrote:

Might give some strange behaviour though:

"1$".to_f => 1.0
"nonsense".to_f => 0.0
"nanotubes".to_f => Nan ?
"inferiority".to_f => Inf ?

Might give some strange behaviour though:

"1$".to_f => 1.0
"nonsense".to_f => 0.0
"nanotubes".to_f => Nan ?
"inferiority".to_f => Inf ?

well, it's not so much "stranger" than the following I suppose

"24/7".to_f => 24.0

Might give some strange behaviour though:

"1$".to_f => 1.0
"nonsense".to_f => 0.0
"nanotubes".to_f => Nan ?
"inferiority".to_f => Inf ?

well, it's not so much "stranger" than the following I suppose

"24/7".to_f => 24.0

On 1.9, try:

"24/7".to_r

=> (24/7)

_.class

=> Rational

-Rob

Rob Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

···

On Aug 11, 2010, at 11:28 AM, Fela Winkelmolen wrote: