# Ruby floating point calculations

**URL:** <https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477>\
**Category:** Rails\
**Created:** [October 17, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477 "2006-10-17T15:01:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stuart\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Stuart\_W](https://www.ruby-forum.com/u/Stuart_W)\
**Post date:** [October 17, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477/1 "2006-10-17T15:01:02Z")

</div>

helo eveyone,

for a rails app i’m working on i need to do some calculations with some  
floating point data. the calculations aren’t on currency so i can’t do  
the  
trick where you do the math with integers and have the units in pennies.  
i  
would like the results to be as accurate as possible with the least  
amount  
of work (haha). any suggestions? is there a ruby gem for more accurate  
float  
calculations?

thanks in advance,

stuart

---

<div class="post-metadata">

**Author:** ![Stuart\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Stuart\_W](https://www.ruby-forum.com/u/Stuart_W)\
**Post date:** [October 17, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477/2 "2006-10-17T15:13:08Z")

</div>

Define accuracy. What are you doing here?

---

<div class="post-metadata">

**Author:** ![Stuart\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Stuart\_W](https://www.ruby-forum.com/u/Stuart_W)\
**Post date:** [October 17, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477/3 "2006-10-17T17:31:09Z")

</div>

calculating ratios mostly, but some other stuff too. for my purposes  
accuracy would be calculations being accurate to .001 … i guess i  
probably just could premultiply the values i’m going to use with 1000  
to account for the precision i’m trying to get and divide by 1000  
after… to tell you the truth i’m not sure how bad the floating  
inacuracy is generally… but i remember seeing some calculations that  
were pretty far off

---

<div class="post-metadata">

**Author:** ![Stuart\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Stuart\_W](https://www.ruby-forum.com/u/Stuart_W)\
**Post date:** [October 17, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477/4 "2006-10-17T17:53:18Z")

</div>

Ah, sounds like you want to use Rational:  
[http://www.ruby-doc.org/stdlib/libdoc/rational/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/rational/rdoc/index.html)

You can also use BigDecimal (require ‘bigdecimal’).

---

<div class="post-metadata">

**Author:** ![Stuart\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9de053/32.png) [@Stuart\_W](https://www.ruby-forum.com/u/Stuart_W)\
**Post date:** [October 17, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/ruby-floating-point-calculations/72477/5 "2006-10-17T17:59:11Z")

</div>

When using Float you get whatever your machine’s native double type  
gives you, which these days is often around 16 significant decimal  
digits.

Fred
