Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rational support for %f in sprintf #2160

headius opened this issue Nov 9, 2014 · 0 comments · Fixed by #5584

Rational support for %f in sprintf #2160

headius opened this issue Nov 9, 2014 · 0 comments · Fixed by #5584


Copy link

headius commented Nov 9, 2014

If I'm reading this right, %f in sprintf gained special handling of Rational values recently.

Here's the test we're failing from mri/ruby/test_sprintf.rb:

  def test_rational
    assert_match(/\A0\.10+\z/, sprintf("%.60f", 0.1r))
    assert_match(/\A0\.010+\z/, sprintf("%.60f", 0.01r))
    assert_match(/\A0\.0010+\z/, sprintf("%.60f", 0.001r))
    assert_match(/\A0\.3+\z/, sprintf("%.60f", 1/3r))
    assert_match(/\A1\.20+\z/, sprintf("%.60f", 1.2r))

    0.upto(9) do |len|
      -1.upto(9) do |prec|
        ['', '+', '-', ' ', '0', '+0', '-0', ' 0', '+ ', '- ', '+ 0', '- 0'].each do |flags|
          fmt = "%#{flags}#{len > 0 ? len : ''}#{prec >= 0 ? ".#{prec}" : ''}f"
          [0, 0.1, 0.01, 0.001, 1.001, 100.0, 100.001, 10000000000.0, 0.00000000001, 1/3r, 2/3r, 1.2r, 10r].each do |num|
            assert_equal(sprintf(fmt, num.to_f), sprintf(fmt, num.to_r), "sprintf(#{fmt.inspect}, #{num.inspect}.to_r)")
            assert_equal(sprintf(fmt, -num.to_f), sprintf(fmt, -num.to_r), "sprintf(#{fmt.inspect}, #{(-num).inspect}.to_r)") if num > 0

The failure occurs in the fourth assert_match:

TestSprintf#test_rational [/Users/headius/projects/jruby/test/mri/ruby/test_sprintf.rb:155]:
Expected /\A0\.3+\z/ to match "0.333333333333333300000000000000000000000000000000000000000000".

There are three commits in MRI that have lines at or after this test: ruby/ruby@1d196e0d, ruby/ruby@28dd6160, and ruby/ruby@230b845f. All three likely have changes we need to incorporate.

Run the test with `jruby test/mri/runner.rb ruby/test_sprintf.rb -n test_rational

@headius headius added the core label Nov 9, 2014
@headius headius added this to the JRuby milestone Nov 9, 2014
@headius headius added beginner and removed beginner labels Nov 9, 2014
@enebo enebo modified the milestone: JRuby Jul 14, 2015
@kares kares added the beginner label Mar 2, 2018
@kares kares self-assigned this Jan 28, 2019
@kares kares removed the beginner label Jan 28, 2019
@kares kares added this to the JRuby milestone Feb 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging a pull request may close this issue.

3 participants