Skip to content

Rational support for %f in sprintf #2160

@headius

Description

@headius

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
          end
        end
      end
    end
  end

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions