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

> 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

irb> Marshal.dump(a)
=> "\x04\bf\bnan"

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

irb> require 'yaml'
=> true
irb> a.to_yaml
=> "--- .NaN\n"
irb> c=YAML.load(a.to_yaml)
=> NaN
irb> c.nan?
=> true

-Rob

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