# Arithmetic operation bug

**URL:** <https://www.ruby-forum.com/t/arithmetic-operation-bug/172647>\
**Category:** Ruby\
**Created:** [July 24, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/arithmetic-operation-bug/172647 "2009-07-24T15:11:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manojchourasia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a587f6/32.png) [@manojchourasia](https://www.ruby-forum.com/u/manojchourasia)\
**Post date:** [July 24, 2009, 3:11pm UTC](https://www.ruby-forum.com/t/arithmetic-operation-bug/172647/1 "2009-07-24T15:11:36Z")

</div>

v1=“128.015”  
puts((v1.to\_f\*1000).to\_i).to\_s // giving 128014 instead of 128015

Why the the result is rounding up to 128014 but the correct result  
should be 128015.

---

<div class="post-metadata">

**Author:** ![manojchourasia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a587f6/32.png) [@manojchourasia](https://www.ruby-forum.com/u/manojchourasia)\
**Post date:** [July 24, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/arithmetic-operation-bug/172647/2 "2009-07-24T16:05:30Z")

</div>

On Friday 24 July 2009, Manoj C. wrote:

> |v1=“128.015”  
> |puts((v1.to\_f\*1000).to\_i).to\_s // giving 128014 instead of 128015  
> |  
> |Why the the result is rounding up to 128014 but the correct result  
> |should be 128015.

I’m not an expert, but I think because when you write 128.015, the  
number you  
actually get is 128.0149999999999863575… (because 128.015 can’t be  
represented as a number with a finite number of decimal digits in  
binary, just  
like, for example, 1/17 can’t be repsented with a finite decimal number  
in  
decimal). You usually don’t see this because by default only a small  
number of  
decimal digits are shown, taking rounding into account. You can obtain a  
greater number of digits using, for example, format:

puts format("%.20f", 128.015)  
=\> 128.01499999999998635758

Now, when you multiply the number by 1000, what you actually get is:  
128014.99999999998544808477. And, since Float#to\_i truncates the number  
(it  
doesn’t round it), you get the number 128014.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![manojchourasia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a587f6/32.png) [@manojchourasia](https://www.ruby-forum.com/u/manojchourasia)\
**Post date:** [July 24, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/arithmetic-operation-bug/172647/3 "2009-07-24T16:06:05Z")

</div>

On Jul 24, 9:11 am, Manoj C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> v1=“128.015”  
> puts((v1.to\_f\*1000).to\_i).to\_s // giving 128014 instead of 128015
> 
> ## Why the the result is rounding up to 128014 but the correct result should be 128015.
> 
> Posted viahttp://www.ruby-forum.com/.

Floats are tricky:  
(120…130).each{|x| puts x,((x+0.015)\*1000).to\_i}

Have a look at “What Every Computer Scientist Should Know About  
Floating-Point Arithmetic” ([http://docs.sun.com/source/806-3568/](http://docs.sun.com/source/806-3568/)  
ncg\_goldberg.html)

---

<div class="post-metadata">

**Author:** ![manojchourasia](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a587f6/32.png) [@manojchourasia](https://www.ruby-forum.com/u/manojchourasia)\
**Post date:** [July 24, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/arithmetic-operation-bug/172647/4 "2009-07-24T17:08:09Z")

</div>

the problem seems to be in to\_i

2009/7/24 Manoj C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
