You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
XPath: mod uses floored modulo instead of truncating remainder #385
The mod operator returns the remainder from a truncating division. For example,
5 mod 2 returns 1
5 mod -2 returns 1
-5 mod 2 returns -1
-5 mod -2 returns -1
libxml2 gives 1, -1 and -0.5.
Cause: lib/rexml/xpath_parser.rb uses left % right. Ruby's % is a floored modulo (the result takes the sign of the right side). left.remainder(right) truncates like the spec asks; it raises ZeroDivisionError for a zero right side, so x mod 0 (and an infinite or NaN left side) needs to keep returning NaN.
@bardrop are you planning to send a PR for this? If not, I have a fix ready: mod uses remainder, and x mod 0 returns NaN. Note that x mod 0 currently raises ZeroDivisionError rather than returning NaN, because Ruby's Float#% raises for a zero divisor, so the fix handles that case explicitly.
Activity
sourya2704 commentedon Nov 20, 2026
@bardrop are you planning to send a PR for this? If not, I have a fix ready:
modusesremainder, andx mod 0returns NaN. Note thatx mod 0currently raisesZeroDivisionErrorrather than returning NaN, because Ruby'sFloat#%raises for a zero divisor, so the fix handles that case explicitly.bardrop commentedon Nov 20, 2026
@sourya2704 Go ahead, I already have two rexml PRs open. Thanks!
sourya2704 commentedon Nov 20, 2026
Thanks! Opened #386.