From: "Jesús Gabriel y Galán" Date: 2010-04-07T20:07:25+09:00 Subject: Re: Precision and == On Wed, Apr 7, 2010 at 1:02 PM, LAMBEAU Bernard wrote: > We all know that finite representation comes with some difficulties... > but... > > x = 45.0*(Math::PI / 180.0) > y = 0.785398163397448 > puts "#{x.class} : #{x}" > puts "#{y.class} : #{y}" > puts 45.0*(Math::PI / 180.0) == 0.785398163397448 > > prints > > Float : 0.785398163397448 > Float : 0.785398163397448 > false > > Does to_s show something less precise than the internal representation of > Floats? Yes, it rounds to 15 decimal positions: irb(main):001:0> x = 45.0*(Math::PI / 180.0) => 0.785398163397448 irb(main):002:0> y = 0.785398163397448 => 0.785398163397448 irb(main):003:0> puts "#{x.class} : #{"%.20f" % x}" Float : 0.78539816339744827900 => nil irb(main):004:0> puts "#{y.class} : #{"%.20f" % y}" Float : 0.78539816339744794593 Jesus.