Is this a bug or a whole new concept?

Discussion in 'Ruby' started by Adam Aa, Mar 2, 2010.

  1. Adam Aa

    Adam Aa Guest

    Greetings,

    I got a book on Ruby and I am trying to learn it piece by piece. I was
    a Perl programmer for a while so the syntax of Ruby is not too hard for
    me. What I don't understand is the following and I thank you in advance
    for your help and input.

    I have a variable named "num" that is set to "9." I'm using the
    num.eql?() method to test whether or not num.eql?(9) and it gives me
    "true" in IRB - no surprise there. The next one below also gives me
    "true" since 18/2 is 9. I read in the book that if you want to force
    the result of an experssion to be a float, make one or both operands a
    floating number. So we have 18/2.0. But the result is "false." How come?
    How come everytime I change either the number to float I get a false
    even though the expression is correct? At first I thought I'm not
    dividing numbers - rather maybe I'm dividing the value of the chars
    rather than their math value. But that's not true as "18.to_i/2.to_i,"
    just like "18/2," gives us "true."

    I'm all ears to hear the logic behind such behavior

    Thanks again

    irb(main):058:0> num.eql?(9)
    => true
    irb(main):059:0> num.eql?(18/2)
    => true
    irb(main):060:0> num.eql?(18/2.0)
    => false
    irb(main):061:0> num.eql?(18.0/2)
    => false
    irb(main):062:0> num.eql?(18.0/2.0)
    => false
    irb(main):063:0> num.eql?(18/2)
    => true
    irb(main):064:0> num.eql?(18.0/2.to_f)
    => false
    irb(main):065:0> num.eql?(18.to_f/2.to_f)
    => false
    irb(main):066:0> num.eql?(18.to_f/2.to_i)
    => false
    irb(main):067:0> num.eql?(18.to_i/2.to_i)
    => true
    irb(main):068:0>
    --
    Posted via http://www.ruby-forum.com/.
    Adam Aa, Mar 2, 2010
    #1
    1. Advertising

  2. Adam Aa

    Adam Aa Guest

    Apologies...should have checked the documentation first..we are also
    comparing types with "eql?"

    num.eql?(numeric) => true or false
    -----------------------------------------------------------------------
    Returns true if num and numeric are the same type and have equal values.

    1 == 1.0 #=> true
    1.eql?(1.0) #=> false
    (1.0).eql?(1.0) #=> true


    Adam Aa wrote:
    >
    > Greetings,
    >
    > I got a book on Ruby and I am trying to learn it piece by piece. I was
    > a Perl programmer for a while so the syntax of Ruby is not too hard for
    > me. What I don't understand is the following and I thank you in advance
    > for your help and input.
    >
    > I have a variable named "num" that is set to "9." I'm using the
    > num.eql?() method to test whether or not num.eql?(9) and it gives me
    > "true" in IRB - no surprise there. The next one below also gives me
    > "true" since 18/2 is 9. I read in the book that if you want to force
    > the result of an experssion to be a float, make one or both operands a
    > floating number. So we have 18/2.0. But the result is "false." How come?
    > How come everytime I change either the number to float I get a false
    > even though the expression is correct? At first I thought I'm not
    > dividing numbers - rather maybe I'm dividing the value of the chars
    > rather than their math value. But that's not true as "18.to_i/2.to_i,"
    > just like "18/2," gives us "true."
    >
    > I'm all ears to hear the logic behind such behavior
    >
    > Thanks again
    >
    > irb(main):058:0> num.eql?(9)
    > => true
    > irb(main):059:0> num.eql?(18/2)
    > => true
    > irb(main):060:0> num.eql?(18/2.0)
    > => false
    > irb(main):061:0> num.eql?(18.0/2)
    > => false
    > irb(main):062:0> num.eql?(18.0/2.0)
    > => false
    > irb(main):063:0> num.eql?(18/2)
    > => true
    > irb(main):064:0> num.eql?(18.0/2.to_f)
    > => false
    > irb(main):065:0> num.eql?(18.to_f/2.to_f)
    > => false
    > irb(main):066:0> num.eql?(18.to_f/2.to_i)
    > => false
    > irb(main):067:0> num.eql?(18.to_i/2.to_i)
    > => true
    > irb(main):068:0>


    --
    Posted via http://www.ruby-forum.com/.
    Adam Aa, Mar 2, 2010
    #2
    1. Advertising

  3. On Mon, Mar 1, 2010 at 8:59 PM, Adam Aa <> wrote:
    >
    > Greetings,
    >
    > I got a book on Ruby and I am trying to learn it piece by piece. =A0I was
    > a Perl programmer for a while so the syntax of Ruby is not too hard for
    > me. =A0What I don't understand is the following and I thank you in advanc=

    e
    > for your help and input.
    >
    > I have a variable named "num" that is set to "9." =A0I'm using the
    > num.eql?() method to test whether or not num.eql?(9) and it gives me
    > "true" in IRB - no surprise there. =A0The next one below also gives me
    > "true" since 18/2 is 9. =A0I read in the book that if you want to force
    > the result of an experssion to be a float, make one or both operands a
    > floating number. So we have 18/2.0. But the result is "false." How come?
    > How come everytime I change either the number to float I get a false
    > even though the expression is correct? =A0At first I thought I'm not
    > dividing numbers - rather maybe I'm dividing the value of the chars
    > rather than their math value. =A0But that's not true as "18.to_i/2.to_i,"
    > just like "18/2," gives us "true."
    >
    > I'm all ears to hear the logic behind such behavior


    #eql? checks to see if the receiver and the parameter have the same
    value. In Ruby, an integer like 9 is guaranteed not to be identical to
    a float like 9.0, even if mathematically they are the same (and the =3D=3D
    method says they are equal). The result of floating-point division
    (i.e. where one or both operands is a float) is a floating-point
    number.

    From <http://ruby-doc.org/core/classes/Object.html#M000347>:


    Equality=97At the Object level, =3D=3D returns true only if obj and other
    are the same object. Typically, this method is overridden in
    descendent classes to provide class-specific meaning.

    Unlike =3D=3D, the equal? method should never be overridden by subclasses:
    it is used to determine object identity (that is, a.equal?(b) iff a is
    the same object as b).

    The eql? method returns true if obj and anObject have the same value.
    Used by Hash to test members for equality. For objects of class
    Object, eql? is synonymous with =3D=3D. Subclasses normally continue this
    tradition, but there are exceptions. Numeric types, for example,
    perform type conversion across =3D=3D, but not across eql?, so:

    1 =3D=3D 1.0 #=3D> true
    1.eql? 1.0 #=3D> false
    Eric Christopherson, Mar 2, 2010
    #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. A.M
    Replies:
    27
    Views:
    570
    Steve Holden
    Jun 29, 2006
  2. \A_Michigan_User\
    Replies:
    2
    Views:
    887
    \A_Michigan_User\
    Aug 21, 2006
  3. Replies:
    1
    Views:
    281
    Roedy Green
    Jul 10, 2007
  4. Dominic

    Added Control & Viewstate: Concept ot Bug?

    Dominic, Dec 3, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    128
    Lostinet.Web Support
    Dec 8, 2003
  5. Replies:
    2
    Views:
    90
Loading...

Share This Page