Weird problem.

Discussion in 'Ruby' started by Erik Terpstra, May 31, 2005.

  1. 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 !!??
     
    Erik Terpstra, May 31, 2005
    #1
    1. Advertising

  2. Erik Terpstra

    Ryan Davis Guest

    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.

    --
    - Seattle.rb - http://www.zenspider.com/
    seattle.rb
    http://blog.zenspider.com/ - http://rubyforge.org/projects/ruby2c
     
    Ryan Davis, May 31, 2005
    #2
    1. Advertising

  3. 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.
     
    Erik Terpstra, May 31, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme
    Replies:
    1
    Views:
    640
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    601
    richard
    Jan 21, 2011
  3. richard
    Replies:
    0
    Views:
    627
    richard
    Jan 21, 2011
  4. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    454
    Captain Paralytic
    Jan 21, 2011
  5. will
    Replies:
    6
    Views:
    433
    Phrogz
    Dec 27, 2006
Loading...

Share This Page