Skip to content

Round half-way values towards positive infinity in ROUND - #81

Open
MathiasVDA wants to merge 1 commit into
DataTreehouse:mainfrom
MathiasVDA:fix/round-ties-towards-positive-infinity
Open

MathiasVDA wants to merge 1 commit into
DataTreehouse:mainfrom
MathiasVDA:fix/round-ties-towards-positive-infinity

Conversation

@MathiasVDA

Copy link
Copy Markdown

Requested by Mathias

Fixes #78.

Before: ROUND rounded negative half-way values away from zero, so ROUND(-2.5) returned -3 and ROUND(-0.5) returned -1.

After: ties go towards positive infinity, as SPARQL 1.1 §17.4.4.2 requires: ROUND(-2.5) = -2, ROUND(-0.5) = 0, ROUND(2.5) = 3. Non-tie values and integers are unchanged.

How: Polars only offers HalfToEven and HalfAwayFromZero. The new sparql_round helper rounds half away from zero, then moves negative ties one step up. A value is a negative tie exactly when x - round(x) == 0.5; that subtraction is exact for ties, also for floats. Integer types are passed through. The helper is used for both single-typed and multi-typed columns. I also removed an unconditional with_column(round(...)) at the top of round_, which every branch below overwrote anyway.

Tests: py_maplib/tests/test_round.py (13 cases: decimal, double and integer literals, plus values read from a graph).

🤖 Generated with Claude Code

SPARQL 1.1 17.4.4.2 (fn:numeric-round): of two equally close integers, the
one closest to positive infinity is returned, so round(-2.5) = -2.
Polars only offers half-away-from-zero, so negative ties are moved one step
up. Refs DataTreehouse#78.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

SPARQL ROUND() rounds negative half-way values away from zero (spec: towards positive infinity)

1 participant