# Why does not this work?

**URL:** <https://rubytalk.org/t/why-does-not-this-work/64070>\
**Category:** ruby-talk\
**Created:** [8 November 2011 04:01 UTC](https://rubytalk.org/t/why-does-not-this-work/64070 "2011-11-08T04:01:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erbay\_Yigit](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@Erbay\_Yigit](https://rubytalk.org/u/Erbay_Yigit)\
**Post date:** [8 November 2011 04:01 UTC](https://rubytalk.org/t/why-does-not-this-work/64070/1 "2011-11-08T04:01:31Z")

</div>

Hi guys,  
I am trying to learn Ruby. I know some Perl. I cannot make following  
script work. Could you please help me understand why "c" value in the  
code does not print. It prints "0" for me. Thank you for help.

code:

> **···**
>
> --------  
> temp1 = 1  
> temp2 = 15
> 
> puts "F:C"
> 
> temp1.upto(temp2) do |i|  
> &nbsp;&nbsp;c = (i-32)\*(5/9)  
> &nbsp;&nbsp;print i,':',c,"\n"  
> end  
> --------
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sam\_Duncan](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Sam\_Duncan](https://rubytalk.org/u/Sam_Duncan)\
**Post date:** [8 November 2011 04:14 UTC](https://rubytalk.org/t/why-does-not-this-work/64070/2 "2011-11-08T04:14:22Z")

</div>

Try 5/9 in irb, and then try 5.0/9, and then you may be closer to where you want to be =]

ruby-1.9.2-p290 :001 \> 5.class.name  
&nbsp;&nbsp;=\> "Fixnum"  
ruby-1.9.2-p290 :002 \> 5.0.class.name  
&nbsp;&nbsp;=\> "Float"

Sam

> **···**
>
> On 08/11/11 17:01, Erbay Yigit wrote:
> 
> > Hi guys,  
> > I am trying to learn Ruby. I know some Perl. I cannot make following  
> > script work. Could you please help me understand why "c" value in the  
> > code does not print. It prints "0" for me. Thank you for help.
> > 
> > code:  
> > --------  
> > temp1 = 1  
> > temp2 = 15
> > 
> > puts "F:C"
> > 
> > temp1.upto(temp2) do |i|  
> > &nbsp;&nbsp;&nbsp;c = (i-32)\*(5/9)  
> > &nbsp;&nbsp;&nbsp;print i,':',c,"\n"  
> > end  
> > --------

---

<div class="post-metadata">

**Author:** ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)\
**Post date:** [8 November 2011 04:15 UTC](https://rubytalk.org/t/why-does-not-this-work/64070/3 "2011-11-08T04:15:37Z")

</div>

> I am trying to learn Ruby. I know some Perl. I cannot make following  
> script work. Could you please help me understand why "c" value in the  
> code does not print. It prints "0" for me. Thank you for help.

> > 5/9

=\> 0

> > 5.0/9.0

=\> 0.555555555555556

> **···**
>
> On Mon, Nov 7, 2011 at 8:01 PM, Erbay Yigit \<erbayyigit@gmail.com\> wrote:
> 
> > >
> 
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
> 
> > **[Hassan Schroeder on about.me](https://about.me/hassanschroeder)**
> >
> > I use about.me to show people what matters most to me.
> 
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [8 November 2011 04:22 UTC](https://rubytalk.org/t/why-does-not-this-work/64070/4 "2011-11-08T04:22:05Z")

</div>

5/9 is integer division, so it evaluates to 0. That is multiplied with i-32 which also evaluates to 0.

The fix is to force floating point division by using at least one floating point literal in the division operation:

c = (i-32)\*(5.0/9)

-Jeremy

> **···**
>
> Erbay Yigit \<[erbayyigit@gmail.com](mailto:erbayyigit@gmail.com)\> wrote:
> 
> Hi guys,  
> I am trying to learn Ruby. I know some Perl. I cannot make following  
> script work. Could you please help me understand why "c" value in the  
> code does not print. It prints "0" for me. Thank you for help.
> 
> code:  
> --------  
> temp1 = 1  
> temp2 = 15
> 
> puts "F:C"
> 
> temp1.upto(temp2) do |i|  
> c = (i-32)\*(5/9)  
> print i,':',c,"\n"  
> end  
> --------
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Erbay\_Yigit](https://avatars.discourse-cdn.com/v4/letter/e/45deac/32.png) [@Erbay\_Yigit](https://rubytalk.org/u/Erbay_Yigit)\
**Post date:** [8 November 2011 04:31 UTC](https://rubytalk.org/t/why-does-not-this-work/64070/5 "2011-11-08T04:31:00Z")

</div>

Thank you for the help guys.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
