# Newbie question

**URL:** <https://www.ruby-forum.com/t/newbie-question/150391>\
**Category:** Ruby\
**Created:** [October 8, 2008, 12:29am UTC](https://www.ruby-forum.com/t/newbie-question/150391 "2008-10-08T00:29:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [October 8, 2008, 12:29am UTC](https://www.ruby-forum.com/t/newbie-question/150391/1 "2008-10-08T00:29:39Z")

</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  
a = 37_i / 50  
c = a + 0.0  
b = (37_i/50.0 - c ) \*50  
if b \< 1.5 && b \> 0.5  
puts i  
puts b  
end  
i = i+1  
end

thanks much  
Will

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [October 8, 2008, 12:47am UTC](https://www.ruby-forum.com/t/newbie-question/150391/2 "2008-10-08T00:47:48Z")

</div>

Rounding errors in floating point arithmetic.

Farrel

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [October 8, 2008, 1:10am UTC](https://www.ruby-forum.com/t/newbie-question/150391/3 "2008-10-08T01:10:37Z")

</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  
[Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point).

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [October 8, 2008, 3:10am UTC](https://www.ruby-forum.com/t/newbie-question/150391/4 "2008-10-08T03:10:43Z")

</div>

On Tue, Oct 7, 2008 at 5:27 PM, Will [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> b = (37\*i/50.0 - c ) \*50  
> if b \< 1.5 && b \> 0.5  
> puts i  
> puts b  
> end  
> i = i+1  
> end

require ‘bigdecimal’

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

Todd
