# Newbie question

**URL:** <https://rubytalk.org/t/newbie-question/49508>\
**Category:** ruby-talk\
**Created:** [7 October 2008 22:27 UTC](https://rubytalk.org/t/newbie-question/49508 "2008-10-07T22:27:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will5](https://avatars.discourse-cdn.com/v4/letter/w/7ab992/32.png) [@Will5](https://rubytalk.org/u/Will5)\
**Post date:** [7 October 2008 22:27 UTC](https://rubytalk.org/t/newbie-question/49508/1 "2008-10-07T22:27:04Z")

</div>

Greetings all, I think this is generally more a programming question than a  
ruby explicit one, however, I am wondering why this code yields 23,  
0.999999999999979 instead of 23, 1.

Any help would be great!

i = 0  
while i \<= 50  
&nbsp;&nbsp;a = 37\*i / 50  
&nbsp;&nbsp;c = a + 0.0  
&nbsp;&nbsp;b = (37\*i/50.0 - c ) \*50  
&nbsp;&nbsp;if b \< 1.5 && b \> 0.5  
&nbsp;&nbsp;&nbsp;&nbsp;puts i  
&nbsp;&nbsp;&nbsp;&nbsp;puts b  
&nbsp;&nbsp;end  
&nbsp;&nbsp;i = i+1  
end

thanks much  
Will

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [7 October 2008 22:45 UTC](https://rubytalk.org/t/newbie-question/49508/2 "2008-10-07T22:45:00Z")

</div>

Rounding errors in floating point arithmetic.

Farrel

> **···**
>
> On 08/10/2008, Will \<wrsh07@gmail.com\> wrote:
> 
> > Greetings all, I think this is generally more a programming question than a  
> > ruby explicit one, however, I am wondering why this code yields 23,  
> > 0.999999999999979 instead of 23, 1.
> > 
> > Any help would be great!
> > 
> > i = 0  
> > while i \<= 50  
> > &nbsp;&nbsp;a = 37\*i / 50  
> > &nbsp;&nbsp;c = a + 0.0  
> > &nbsp;&nbsp;b = (37\*i/50.0 - c ) \*50  
> > &nbsp;&nbsp;if b \< 1.5 && b \> 0.5  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts i  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts b  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;i = i+1  
> > end
> > 
> > thanks much  
> > Will
> 
> --  
> Aimred - Ruby Development and Consulting
> 
> > **[Aimred - Home - Ruby and Rails Development in Cape Town, South
> Africa](http://www.aimred.com)**
> >
> > Aimred is a specialist Ruby and Ruby on Rails development house and consultancy based in Cape Town, South Africa.

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [7 October 2008 23:07 UTC](https://rubytalk.org/t/newbie-question/49508/3 "2008-10-07T23:07:50Z")

</div>

Will wrote:

> Greetings all, I think this is generally more a programming question than a  
> ruby explicit one, however, I am wondering why this code yields 23,  
> 0.999999999999979 instead of 23, 1.

A common question. Check out [http://en.wikipedia.org/wiki/Floating\_point\](http://en.wikipedia.org/wiki/Floating_point%5C).

> **···**
>
> --  
> RMagick: [http://rmagick.rubyforge.org/](http://rmagick.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [8 October 2008 01:08 UTC](https://rubytalk.org/t/newbie-question/49508/4 "2008-10-08T01:08:10Z")

</div>

require 'bigdecimal'

#put a bigdecimal in the numerator or denominator...  
((BigDecimal("37") \* 23 / 50 - 17) \* 50).to\_f  
#=\> 1.0

Todd

> **···**
>
> On Tue, Oct 7, 2008 at 5:27 PM, Will \<wrsh07@gmail.com\> wrote:
> 
> > Greetings all, I think this is generally more a programming question than a  
> > ruby explicit one, however, I am wondering why this code yields 23,  
> > 0.999999999999979 instead of 23, 1.
> > 
> > Any help would be great!
> > 
> > i = 0  
> > while i \<= 50  
> > a = 37\*i / 50  
> > c = a + 0.0  
> > b = (37\*i/50.0 - c ) \*50  
> > if b \< 1.5 && b \> 0.5  
> > &nbsp;&nbsp;&nbsp;puts i  
> > &nbsp;&nbsp;&nbsp;puts b  
> > end  
> > i = i+1  
> > end
