# Ruby Noob issue

**URL:** <https://www.ruby-forum.com/t/ruby-noob-issue/176134>\
**Category:** Ruby\
**Created:** [September 25, 2009, 3:07am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134 "2009-09-25T03:07:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fim32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fim32](https://www.ruby-forum.com/u/fim32)\
**Post date:** [September 25, 2009, 3:07am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134/1 "2009-09-25T03:07:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fim32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fim32](https://www.ruby-forum.com/u/fim32)\
**Post date:** [September 25, 2009, 3:11am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134/2 "2009-09-25T03:11:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fim32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fim32](https://www.ruby-forum.com/u/fim32)\
**Post date:** [September 25, 2009, 3:17am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134/3 "2009-09-25T03:17:51Z")

</div>

Satish T. 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.

---

<div class="post-metadata">

**Author:** ![fim32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fim32](https://www.ruby-forum.com/u/fim32)\
**Post date:** [September 25, 2009, 8:01am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134/4 "2009-09-25T08:01:54Z")

</div>

Welcome to Ruby, Fred!

On 09/25/2009 03:17 AM, Fred I. wrote:

> Satish T. 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

---

<div class="post-metadata">

**Author:** ![fim32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fim32](https://www.ruby-forum.com/u/fim32)\
**Post date:** [September 26, 2009, 12:15am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134/5 "2009-09-26T00:15:25Z")

</div>

Hi,

Am Freitag, 25. Sep 2009, 10:17:53 +0900 schrieb Fred I.:

> Satish T. 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

---

<div class="post-metadata">

**Author:** ![fim32](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fim32](https://www.ruby-forum.com/u/fim32)\
**Post date:** [September 25, 2009, 9:26am UTC](https://www.ruby-forum.com/t/ruby-noob-issue/176134/6 "2009-09-25T09:26:46Z")

</div>

On 2009-09-25, Fred I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
