Ruby Noob issue

Discussion in 'Ruby' started by Fred Ige, Sep 25, 2009.

  1. Fred Ige

    Fred Ige Guest

    Hi
    I'm really new to Ruby
    as such it took me about an hour to register ^^'...
    That being said

    I am trying to figure out why this program I made keeps returning as "0"

    def f(x)
    return (5/9) * (x-32)
    end
    x=1001
    print(f(x))

    Not complicated at all
    I'm just intending for the x value 1001 to plug its self into the
    temperature equation and print that value

    Thanks for reading
    --
    Posted via http://www.ruby-forum.com/.
    Fred Ige, Sep 25, 2009
    #1
    1. Advertising

  2. Fred Ige

    Satish Talim Guest

    [Note: parts of this message were removed to make it a legal post.]

    Fred, (5/9) is integer division. The decimal part is truncated leaving you
    with 0. Use (5.0/9) instead.
    Satish

    On Fri, Sep 25, 2009 at 6:37 AM, Fred Ige <> wrote:

    > Hi
    > I'm really new to Ruby
    > as such it took me about an hour to register ^^'...
    > That being said
    >
    > I am trying to figure out why this program I made keeps returning as "0"
    >
    > def f(x)
    > return (5/9) * (x-32)
    > end
    > x=1001
    > print(f(x))
    >
    > Not complicated at all
    > I'm just intending for the x value 1001 to plug its self into the
    > temperature equation and print that value
    >
    > Thanks for reading
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Satish Talim, Sep 25, 2009
    #2
    1. Advertising

  3. Fred Ige

    Fred Ige Guest

    Satish Talim wrote:
    > Fred, (5/9) is integer division. The decimal part is truncated leaving
    > you
    > with 0. Use (5.0/9) instead.
    > Satish


    Thanks! It works now
    I cant believe such a small issue throw me off for so long.
    --
    Posted via http://www.ruby-forum.com/.
    Fred Ige, Sep 25, 2009
    #3
  4. Welcome to Ruby, Fred!

    On 09/25/2009 03:17 AM, Fred Ige wrote:
    > Satish Talim wrote:
    >> Fred, (5/9) is integer division. The decimal part is truncated leaving
    >> you
    >> with 0. Use (5.0/9) instead.
    >> Satish

    >
    > Thanks! It works now
    > I cant believe such a small issue throw me off for so long.


    Well, the good news is that you learned it early the hard way and won't
    forget it anytime soon. That will help you avoid such errors in more
    complex code where they are more difficult to spot. :)

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Sep 25, 2009
    #4
  5. Fred Ige

    Seebs Guest

    On 2009-09-25, Fred Ige <> wrote:
    > Thanks! It works now
    > I cant believe such a small issue throw me off for so long.


    Was it more than two days?

    If not, I think you're ahead of the curve. The real question is how long
    it will take you to figure out why some similar expression is coming out
    0 in three or four years. :)

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Sep 25, 2009
    #5
  6. Hi,

    Am Freitag, 25. Sep 2009, 10:17:53 +0900 schrieb Fred Ige:
    > Satish Talim wrote:
    > > Fred, (5/9) is integer division. The decimal part is truncated leaving
    > > you
    > > with 0. Use (5.0/9) instead.
    > > Satish

    >
    > Thanks! It works now
    > I cant believe such a small issue throw me off for so long.


    Actually, you're not the first one to stumble over the
    integer/floating point problem.

    As expressions are evaluated from left to right, the first number
    coerces the value to be Float. You could also say:

    (x.to_f - 32) * 5 / 9

    Or

    class Numeric
    def f2c
    (to_f - 32) * 5 / 9
    end
    end

    -32.f2c #=> 0.0


    Here's another pitfall about floating point values. Be aware that
    aren't exact in general.

    "%20.18f" % 0.3 #=> "0.299999999999999989"

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Sep 25, 2009
    #6
    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. Jeff Carlson
    Replies:
    9
    Views:
    117
    James Edward Gray II
    Oct 12, 2005
  2. Eric Luo
    Replies:
    4
    Views:
    97
    Eric Luo
    Mar 29, 2006
  3. Replies:
    7
    Views:
    329
    Jan Svitok
    Mar 2, 2007
  4. mosfet

    ruby noob

    mosfet, Mar 5, 2007, in forum: Ruby
    Replies:
    2
    Views:
    97
    Jan Svitok
    Mar 5, 2007
  5. Philip Amadeo Saeli

    Noob Q: ruby block scoping question (ruby TK)

    Philip Amadeo Saeli, Apr 30, 2008, in forum: Ruby
    Replies:
    4
    Views:
    131
    Joel VanderWerf
    May 1, 2008
Loading...

Share This Page