From: Diogo Almeida on 13 Jul 2010 08:09 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 -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 13 Jul 2010 08:36 2010/7/13 Diogo Almeida <diogo.borges.almeida(a)gmail.com>: > 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 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Diogo Almeida on 13 Jul 2010 09:15 Robert Klemme wrote: > 2010/7/13 Diogo Almeida <diogo.borges.almeida(a)gmail.com>: > > 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. -- Posted via http://www.ruby-forum.com/.
From: Caleb Clausen on 13 Jul 2010 10:49 On 7/13/10, Diogo Almeida <diogo.borges.almeida(a)gmail.com> wrote: > Robert Klemme wrote: >> 2010/7/13 Diogo Almeida <diogo.borges.almeida(a)gmail.com>: >> >> 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. 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.
From: Diogo Almeida on 13 Jul 2010 14:30 Thanks guys -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: How to install 1.9.2 on windows 7 Next: Programming TCP-protocol Server (MineCraft) |