same operation (sort of), different results

Discussion in 'Ruby' started by Chad Perrin, Mar 30, 2007.

  1. Chad Perrin

    Chad Perrin Guest

    I'm a little confused by these results. Perhaps someone can tell me
    what I'm assuming incorrectly:

    $ irb
    irb(main):001:0> 5 / 9 * ( 100 - 32 )
    => 0
    irb(main):002:0> ( 5 / 9 ) * ( 100 - 32 )
    => 0
    irb(main):004:0> ( 100 - 32 ) * ( 5 / 9 )
    => 0
    irb(main):003:0> ( 100 - 32 ) * 5 / 9
    => 37

    The last result is the only one that gives me what I actually wanted.

    In case you're wondering, yes, that *is* an F-to-C temperature
    conversion.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "The ability to quote is a serviceable
    substitute for wit." - W. Somerset Maugham
     
    Chad Perrin, Mar 30, 2007
    #1
    1. Advertising

  2. Chad Perrin

    Chad Perrin Guest

    On Fri, Mar 30, 2007 at 02:30:33PM +0900, Chad Perrin wrote:
    > I'm a little confused by these results. Perhaps someone can tell me
    > what I'm assuming incorrectly:
    >
    > $ irb
    > irb(main):001:0> 5 / 9 * ( 100 - 32 )
    > => 0
    > irb(main):002:0> ( 5 / 9 ) * ( 100 - 32 )
    > => 0
    > irb(main):004:0> ( 100 - 32 ) * ( 5 / 9 )
    > => 0
    > irb(main):003:0> ( 100 - 32 ) * 5 / 9
    > => 37
    >
    > The last result is the only one that gives me what I actually wanted.
    >
    > In case you're wondering, yes, that *is* an F-to-C temperature
    > conversion.


    Never mind, that was a full-on brain fart. I forgot I was doing integer
    division. Mea culpa.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    Amazon.com interview candidate: "When C++ is your
    hammer, everything starts to look like your thumb."
     
    Chad Perrin, Mar 30, 2007
    #2
    1. Advertising

  3. Chad Perrin

    John Joyce Guest

    Yep, you can do integer division but do it last for best results.
    you can also throw a .to_f method onto everything for your division..
    The only way around real division errors is to use large integers,
    keep track of decimal location in your own custom object for display
    purposes and thus push the division error way way down to a small,
    insignificant number.
    On Mar 30, 2007, at 2:34 PM, Chad Perrin wrote:

    > On Fri, Mar 30, 2007 at 02:30:33PM +0900, Chad Perrin wrote:
    >> I'm a little confused by these results. Perhaps someone can tell me
    >> what I'm assuming incorrectly:
    >>
    >> $ irb
    >> irb(main):001:0> 5 / 9 * ( 100 - 32 )
    >> => 0
    >> irb(main):002:0> ( 5 / 9 ) * ( 100 - 32 )
    >> => 0
    >> irb(main):004:0> ( 100 - 32 ) * ( 5 / 9 )
    >> => 0
    >> irb(main):003:0> ( 100 - 32 ) * 5 / 9
    >> => 37
    >>
    >> The last result is the only one that gives me what I actually wanted.
    >>
    >> In case you're wondering, yes, that *is* an F-to-C temperature
    >> conversion.

    >
    > Never mind, that was a full-on brain fart. I forgot I was doing
    > integer
    > division. Mea culpa.
    >
    > --
    > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    > Amazon.com interview candidate: "When C++ is your
    > hammer, everything starts to look like your thumb."
    >
     
    John Joyce, Mar 30, 2007
    #3
  4. On 30.03.2007 08:10, John Joyce wrote:
    > Yep, you can do integer division but do it last for best results.
    > you can also throw a .to_f method onto everything for your division..
    > The only way around real division errors is to use large integers, keep
    > track of decimal location in your own custom object for display purposes
    > and thus push the division error way way down to a small, insignificant
    > number.


    There is another way: BigDecimal.

    robert
     
    Robert Klemme, Mar 30, 2007
    #4
  5. Chad Perrin

    John Joyce Guest

    I meant in general. For portability and dealing with float math.
    Lots of solutions exist of course. One of them is Ruby's BigDecimal
    On Mar 30, 2007, at 4:20 PM, Robert Klemme wrote:

    > On 30.03.2007 08:10, John Joyce wrote:
    >> Yep, you can do integer division but do it last for best results.
    >> you can also throw a .to_f method onto everything for your
    >> division..
    >> The only way around real division errors is to use large integers,
    >> keep track of decimal location in your own custom object for
    >> display purposes and thus push the division error way way down to
    >> a small, insignificant number.

    >
    > There is another way: BigDecimal.
    >
    > robert
    >
     
    John Joyce, Mar 30, 2007
    #5
  6. Chad Perrin

    Chad Perrin Guest

    On Fri, Mar 30, 2007 at 03:10:48PM +0900, John Joyce wrote:
    > Yep, you can do integer division but do it last for best results.
    > you can also throw a .to_f method onto everything for your division..
    > The only way around real division errors is to use large integers,
    > keep track of decimal location in your own custom object for display
    > purposes and thus push the division error way way down to a small,
    > insignificant number.


    Thanks. I knew all this -- I just managed to completely forget
    everything of use yesterday (the first day after a debilitating
    migraine took me out of action for most of the day). Apparently, I
    became temporarily very stupid as an after-effect. Unfortunately,
    some of the evidence of this made it to the ruby-talk mailing list.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "A script is what you give the actors. A program
    is what you give the audience." - Larry Wall
     
    Chad Perrin, Mar 30, 2007
    #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. Christopher Brewster
    Replies:
    5
    Views:
    345
    John Machin
    Nov 14, 2008
  2. bluebaron
    Replies:
    3
    Views:
    743
    Jonathan N. Little
    Nov 4, 2009
  3. Navin
    Replies:
    1
    Views:
    699
    Ken Schaefer
    Sep 9, 2003
  4. Guest
    Replies:
    2
    Views:
    179
    Foo Man Chew
    Dec 29, 2003
  5. none
    Replies:
    1
    Views:
    78
Loading...

Share This Page