Skip to content

Format small floats without scientific notation in XPath string() - #395

Merged
naitoh merged 2 commits into
ruby:masterfrom
mokshupadhyay:fix/xpath-string-decimal-format
Dec 1, 2026
Merged

naitoh merged 2 commits into
ruby:masterfrom
mokshupadhyay:fix/xpath-string-decimal-format

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

The XPath string() function formats a number that is not an integer with Ruby's Float#to_s, which switches to scientific notation for small values. XPath 1.0 has no exponent syntax for numbers (section 4.2 describes the number-to-string conversion in decimal form), and number() cannot read such a string back:

doc = REXML::Document.new("<a/>")
REXML::XPath.first(doc, "string(0.00001)")                 # => "1.0e-05"
REXML::XPath.first(doc, "string(0.0000123)")               # => "1.23e-05"
REXML::XPath.first(doc, "string(1 div 10000000000)")       # => "1.0e-10"
REXML::XPath.first(doc, "number(string(0.00001))")         # => NaN

After this change a value whose to_s has a negative exponent is written in decimal form ("0.00001", "0.0000123", "0.0000000001", and "-0.00001" for a negative one), so number(string(x)) gives x back. Integers, and floats whose to_s has no exponent, are unchanged.

Test: test_string_small_float in test/functions/test_base.rb fails on master (["0.00001", ...] expected but ["1.0e-05", ...] found, and NaN for the round trip) and passes now. bundle exec rake test passes (872 tests, 0 failures, 0 errors, 5 omissions). Ruby 3.4.5; the repo has no RuboCop configured here.

Comment thread test/functions/test_base.rb Outdated
Comment on lines +259 to +266
assert_equal(["0.00001", "-0.00001", "0.0000123", "0.0000000001", 0.00001],
[
REXML::XPath.first(doc, "string(0.00001)"),
REXML::XPath.first(doc, "string(-0.00001)"),
REXML::XPath.first(doc, "string(0.0000123)"),
REXML::XPath.first(doc, "string(1 div 10000000000)"),
REXML::XPath.first(doc, "number(string(0.00001))"),
])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using individual assert_equal calls makes it easier to see which expected value corresponds to which XPath expression:

Suggested change
assert_equal(["0.00001", "-0.00001", "0.0000123", "0.0000000001", 0.00001],
[
REXML::XPath.first(doc, "string(0.00001)"),
REXML::XPath.first(doc, "string(-0.00001)"),
REXML::XPath.first(doc, "string(0.0000123)"),
REXML::XPath.first(doc, "string(1 div 10000000000)"),
REXML::XPath.first(doc, "number(string(0.00001))"),
])
assert_equal("0.00001", REXML::XPath.first(doc, "string(0.00001)"))
assert_equal("-0.00001", REXML::XPath.first(doc, "string(-0.00001)"))
assert_equal("0.0000123", REXML::XPath.first(doc, "string(0.0000123)"))
assert_equal("0.0000000001", REXML::XPath.first(doc, "string(1 div 10000000000)"))
assert_equal(0.00001, REXML::XPath.first(doc, "number(string(0.00001))"))

@mokshupadhyay

Copy link
Copy Markdown
Contributor Author

Thanks, applied: test_string_small_float now uses one assert_equal per expression, exactly as suggested. On master the first assertion fails with "0.00001" expected but was ...; with the fix the file passes (28 tests, 141 assertions).

@naitoh naitoh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@naitoh
naitoh merged commit f3ba580 into ruby:master Dec 1, 2026
71 checks passed
@naitoh

naitoh commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants