# Ruby float modulus operation

**URL:** <https://www.ruby-forum.com/t/ruby-float-modulus-operation/191414>\
**Category:** Ruby\
**Created:** [July 12, 2010, 10:20am UTC](https://www.ruby-forum.com/t/ruby-float-modulus-operation/191414 "2010-07-12T10:20:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karthick\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@Karthick\_S](https://www.ruby-forum.com/u/Karthick_S)\
**Post date:** [July 12, 2010, 10:20am UTC](https://www.ruby-forum.com/t/ruby-float-modulus-operation/191414/1 "2010-07-12T10:20:35Z")

</div>

Hi,

I am new to ruby and found this while experimenting with ruby.

irb(main):010:0\> 5.1%0.5  
=\> 0.0999999999999996  
irb(main):011:0\>

Is this similar to the float related operations that MS Excel has?

- Karthick S.

---

<div class="post-metadata">

**Author:** ![Karthick\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@Karthick\_S](https://www.ruby-forum.com/u/Karthick_S)\
**Post date:** [July 12, 2010, 12:15pm UTC](https://www.ruby-forum.com/t/ruby-float-modulus-operation/191414/2 "2010-07-12T12:15:34Z")

</div>

Karthick S. wrote:

> I am new to ruby and found this while experimenting with ruby.
> 
> irb(main):010:0\> 5.1%0.5  
> =\> 0.0999999999999996

Ruby is no different to any other language which uses binary floating  
point arithmetic:

$ perl -e ‘print 5.1 - 10\*0.5, “\n”’  
0.0999999999999996

The problem is that 1/10 does not have an exact representation in binary  
floating point, in the same way that 1/3 does not have an exact  
representation in decimal floating point (0.33333…)

To learn more see [http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)

For some applications, you might use the Rational or BigDecimal classes  
instead. If dealing with currency, you can work in Integers for  
cents/pence etc.

Or maybe you just want to use fewer significant digits when printing:

> > v = 5.1 % 0.5  
> > =\> 0.0999999999999996  
> > “%.8f” % v  
> > =\> “0.10000000”

although you’re still likely to be tripped up by this:

> > v == 0.1  
> > =\> false

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![Karthick\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a88e57/32.png) [@Karthick\_S](https://www.ruby-forum.com/u/Karthick_S)\
**Post date:** [July 12, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/ruby-float-modulus-operation/191414/3 "2010-07-12T16:00:44Z")

</div>

On Mon, Jul 12, 2010 at 6:15 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Karthick S. wrote:
> 
> > I am new to ruby and found this while experimenting with ruby.
> > 
> > irb(main):010:0\> 5.1%0.5  
> > =\> 0.0999999999999996
> 
> Ruby is no different to any other language which uses binary floating  
> point arithmetic

It isn’t relevant to the OPs example, but not all languages produce  
the results for modulo when presented with negative arguments:

> **[Modulo | Common pitfalls](https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls)**
>
> When the result of a modulo operation has the sign of the dividend (truncated definition), it can lead to surprising mistakes.
> For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1:
> But in a language where modulo has the sign of the dividend, that is incorrect, because when n (the dividend) is negative and odd, n mod 2 returns −1, and the function returns false.
> One correct alternative is to test that the remainder is not 0 (because remain...

Ruby, Perl, and MS Excel all return a result with the sign of the  
divisor, while Java returns a result with the sign of the dividend.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
