This repository was archived by the owner on May 25, 2026. It is now read-only.
fix: use Decimal.quantize for base-10 rounding precision#291
Open
DukeDeSouth wants to merge 2 commits into
Open
fix: use Decimal.quantize for base-10 rounding precision#291DukeDeSouth wants to merge 2 commits into
DukeDeSouth wants to merge 2 commits into
Conversation
round_down(9.53, 2) returns 9.52 because floor(9.53 * 100) = floor(952.999...) = 952 in IEEE 754. Same class of bug affects round_up, round_normal, and to_token_decimals. Switched all four functions from float arithmetic (floor/ceil) to Decimal.quantize with ROUND_FLOOR/ROUND_CEILING/ROUND_HALF_UP. Decimal is already imported in helpers.py but was only used in decimal_places(). No new dependencies, signatures unchanged. Closes Polymarket#142 Made-with: Cursor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
round_normal() previously used Python's round() which implements banker's rounding (ROUND_HALF_EVEN). The initial Decimal.quantize fix incorrectly used ROUND_HALF_UP, changing midpoint behavior (e.g. 0.25 at 1dp: 0.2 vs 0.3). Fixed per Bugbot review. Made-with: Cursor
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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_down(9.53, 2) currently returns 9.52 because floor(9.53 * 100) hits
IEEE 754 representation (952.999...) and floors to 952. Same issue in
round_up, round_normal, and to_token_decimals.
This replaces float arithmetic with Decimal(str(x)).quantize() — the Decimal
import was already there for decimal_places() but unused for rounding. No new
deps, no signature changes, all 82 existing tests pass.
Closes #142
Made with Cursor
Note
Medium Risk
Changes rounding and amount conversion used to compute on-chain order amounts; small numeric behavior shifts could affect order sizing/price edge cases despite being a targeted fix for float precision issues.
Overview
Fixes float precision edge cases in order amount/price rounding by replacing
floor/ceil/round-based math withDecimal(str(x)).quantize(...)inround_down,round_normal, andround_up.Simplifies
to_token_decimalsto convert to 6-decimal token units viaDecimalmultiplication andROUND_HALF_EVENquantization, removing the priordecimal_places/extra rounding step so order amount calculations are deterministic in base-10.Written by Cursor Bugbot for commit 5a323da. This will update automatically on new commits. Configure here.