Skip to content

Use a truncating remainder for XPath mod - #386

Open
sourya2704 wants to merge 1 commit into
ruby:masterfrom
sourya2704:xpath-mod-truncating-remainder
Open

sourya2704 wants to merge 1 commit into
ruby:masterfrom
sourya2704:xpath-mod-truncating-remainder

Conversation

@sourya2704

Copy link
Copy Markdown

Fixes #385.

XPath 1.0 defines mod as the remainder from a truncating division, but the parser used Ruby's %, which floors, so the sign followed the right operand. This uses Numeric#remainder instead:

spec before after
5 mod -2 1 -1 1
-5 mod 2 -1 1 -1
-0.5 mod 3 -0.5 2.5 -0.5

x mod 0 raised ZeroDivisionError, because Float#% and Float#remainder both raise for a zero divisor. It now returns NaN, like div by zero already returns Infinity or NaN.

The new tests use the spec's examples and fail without the change.