[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Weird problem.

Erik Terpstra

5/31/2005 9:06:00 AM

Why is the last expression false?


class Part
attr_reader :x, :y, :x2, :y2

def height
y2 - y
end

def initialize(x, y, x2, y2)
@x, @y, @x2, @y2 = x, y, x2, y2
end
end

part = Part.new(200.40, 248.32, 417.0, 306.52)

puts "part.height == #{part.height.inspect}" # part.height == 58.2

p part.height == 58.2 #-> false !!??
2 Answers

Ryan Davis

5/31/2005 9:17:00 AM

0


On May 31, 2005, at 2:10 AM, Erik Terpstra wrote:

> Why is the last expression false?
> ...
> p part.height == 58.2 #-> false !!??

Same reason in pretty much every other language. Floating point
arithmetic is imprecise.

p part.height - 58.2 #-> -1.4210854715202e-14

If you look in test/unit/assertions.rb you'll find assert_in_delta
(f1, f2, d) for exactly this reason.

--
ryand-ruby@zenspider.com - Seattle.rb - http://www.zens...
seattle.rb
http://blog.zens... - http://rubyforge.org/proje...



Erik Terpstra

5/31/2005 9:24:00 AM

0

Ryan Davis wrote:
> Same reason in pretty much every other language. Floating point
> arithmetic is imprecise.
>
> p part.height - 58.2 #-> -1.4210854715202e-14
>
> If you look in test/unit/assertions.rb you'll find assert_in_delta (f1,
> f2, d) for exactly this reason.

Thanks a lot, I should have known this.

Cheers,

Erik.