fix: round() and mod follow the XPath 1.0 numeric spec - #128
Merged
Conversation
round() rounded halves away from zero and dropped the non-finite cases via the int cast (REC 4.4); it now returns a number like ceiling()/floor(), so halves round toward +Infinity and NaN/+-Infinity pass through. Since it no longer needs the go1.10-only math.Round it moves next to roundFunc. mod truncated its operands to int and panicked on a zero divisor; math.Mod is the truncating remainder the spec requires (REC 3.5), with NaN for mod by zero.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
round() and the mod operator both diverge from XPath 1.0 (and from libxml2/lxml).
round() — REC §4.4
int(math.Round(f))rounds halves away from zero and drops non-finite values:round(-0.5)→ -1 (want 0),round(-1.5)→ -2 (want -1),round(-2.5)→ -3 (want -2); §4.4 rounds halves toward +Infinity.round(1 div 0)→ 9223372036854775807,round(-1 div 0)→ the int64 minimum,round(0 div 0)→ 0; want Infinity / -Infinity / NaN.round(2.5) + 0.5evaluates to NaN instead of 3.5 (asNumber only understands float64).round() now returns float64 like ceiling()/floor(): NaN/±Infinity pass through, and finite values use
floor(f)with a half-up carry. It no longer depends on the go1.10-onlymath.Round, so it moves next to roundFunc. (§4.4 notes thatfloor(f + 0.5)is wrong for the signed-zero cases, e.g.round(-0.4)is -0.)mod — REC §3.5
float64(int(a) % int(b))truncates the operands and panics on a zero divisor:5.5 mod 2→ 1 (want 1.5) and5 mod 0panics with "integer divide by zero" (want NaN).math.Modis the truncating IEEE remainder the spec calls for — the sign follows the dividend, and a zero divisor yields NaN.All results checked against lxml/libxml2. The existing round tests are updated to the number result, and table tests are added for both classes covering the half-to-+Infinity, non-finite, and zero-divisor edges.
go test ./...,go test -race ./...,go vet ./...,git diff --check.