# Ruby 1.9.2 float precision

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495>\
**Category:** Ruby\
**Created:** [July 13, 2010, 2:09pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495 "2010-07-13T14:09:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 13, 2010, 2:09pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/1 "2010-07-13T14:09:01Z")

</div>

Hello,

The Liquid templating engine has the following tests passing till ruby  
1.9.2 rc1:

assert\_template\_result “4.66666666666667”, “{{ 14 | divided\_by:‘3.0’ }}”  
#equals to 14/3.0 in irb  
assert\_template\_result “6.3”, “{{ ‘2.1’ | times:3 }}” #equals to 2.1\*3  
in irb

However, as far as I could understand, the float precision has been  
changed with RC1 and the first assertion now returns 15 decimal digits  
instead of 14:  
ruby-1.9.2-rc1 \> 14/3.0  
=\> 4.666666666666667

And the second returns the same 15 decimal digits instead of 1:  
ruby-1.9.2-rc1 \> 2.1\*3  
=\> 6.300000000000001

Even though I realize that the templating engine should not be messing  
with the ruby pre-defined precisions, I would like to ask what would be  
the recommended way to get all ruby versions to behave the same?

For instance, I’ve noticed that in both 1.9.1 and 1.9.2-rc1 the  
instruction (14/3.0).to\_r returns the exact same rational, just  
differing when using the #to\_f

Best regards

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 13, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/2 "2010-07-13T14:40:07Z")

</div>

2010/7/13 Diogo A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> instead of 14:
> 
> For instance, I’ve noticed that in both 1.9.1 and 1.9.2-rc1 the  
> instruction (14/3.0).to\_r returns the exact same rational, just  
> differing when using the #to\_f

I question the validity of the test. Expecting a particular  
formatting of a float number without giving a concrete format is at  
least fragile (as you have discovered) - I would even call it wrong.  
Different than for integers there is no common agreed way to convert  
floats to strings so if you want a particular formatting you need to  
make it part of the test. An alternative would be a regexp match  
against \A4.6+7\z

irb(main):001:0\> /\A4.6+7\z/ =~ (14.0/3).to\_s  
=\> 0

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 13, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/3 "2010-07-13T15:15:38Z")

</div>

Robert K. wrote:

> 2010/7/13 Diogo A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> I question the validity of the test. Expecting a particular  
> formatting of a float number without giving a concrete format is at  
> least fragile (as you have discovered) - I would even call it wrong.  
> Different than for integers there is no common agreed way to convert  
> floats to strings so if you want a particular formatting you need to  
> make it part of the test. An alternative would be a regexp match  
> against \A4.6+7\z
> 
> irb(main):001:0\> /\A4.6+7\z/ =~ (14.0/3).to\_s  
> =\> 0

Yes, I found it odd when I saw it myself. Though, since this was a  
recent change I figured someone could know how to fallback to the “old  
formatting”, without having to use regex, or other alternative  
approaches.

Best regards.

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 13, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/4 "2010-07-13T20:30:13Z")

</div>

Thanks guys

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 13, 2010, 4:51pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/5 "2010-07-13T16:51:19Z")

</div>

On 7/13/10, Diogo A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > irb(main):001:0\> /\A4.6+7\z/ =~ (14.0/3).to\_s  
> > =\> 0
> 
> Yes, I found it odd when I saw it myself. Though, since this was a  
> recent change I figured someone could know how to fallback to the “old  
> formatting”, without having to use regex, or other alternative  
> approaches.

1.9.2 has been changed so that float.to\_s.to\_f always round-trips with  
a minimal string representation. This was not true in the past. This  
new behavior is better; there’s no flag or anything to get the old  
behavior back. I suggest you follow Robert’s suggestions for fixing  
the test.

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 14, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/6 "2010-07-14T16:27:41Z")

</div>

> Even though I realize that the templating engine should not be messing  
> with the ruby pre-defined precisions, I would like to ask what would be  
> the recommended way to get all ruby versions to behave the same?

I think it’s something like this for \<= 1.9.1

class Float  
def to\_s  
num = “%.12f” % self  
num.sub /.0+$/, ‘’  
end  
end

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [July 14, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/7 "2010-07-14T18:08:33Z")

</div>

Roger P. wrote:

> > Even though I realize that the templating engine should not be messing  
> > with the ruby pre-defined precisions, I would like to ask what would be  
> > the recommended way to get all ruby versions to behave the same?
> 
> I think it’s something like this for \<= 1.9.1
> 
> class Float  
> def to\_s  
> num = “%.12f” % self  
> num.sub /.0+$/, ‘’  
> end  
> end

Thanks Roger. I’ll wait and see if the guys from shopify will accept my  
new assertions based on regexp, so they don’t change the new behaviors  
introduced with the latest versions.

Best regards.

---

<div class="post-metadata">

**Author:** ![diabolic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@diabolic](https://www.ruby-forum.com/u/diabolic)\
**Post date:** [August 27, 2010, 12:32am UTC](https://www.ruby-forum.com/t/ruby-1-9-2-float-precision/191495/8 "2010-08-27T00:32:53Z")

</div>

Here’s a writeup of why floats are different in 1.9.2:

[http://wiki.github.com/rdp/ruby\_tutorials\_core/ruby-talk-faq#floats\_imprecise](http://wiki.github.com/rdp/ruby_tutorials_core/ruby-talk-faq#floats_imprecise)

Enjoy.  
-r
