[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

same operation (sort of), different results

Chad Perrin

3/30/2007 5:31:00 AM

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.ap... ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham

5 Answers

Chad Perrin

3/30/2007 5:35:00 AM

0

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.ap... ]
Amazon.com interview candidate: "When C++ is your
hammer, everything starts to look like your thumb."

John Joyce

3/30/2007 6:11:00 AM

0

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.ap... ]
> Amazon.com interview candidate: "When C++ is your
> hammer, everything starts to look like your thumb."
>


Robert Klemme

3/30/2007 7:18:00 AM

0

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

3/30/2007 11:40:00 AM

0

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
>


Chad Perrin

3/30/2007 4:04:00 PM

0

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.ap... ]
"A script is what you give the actors. A program
is what you give the audience." - Larry Wall