Skip to content
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
Polymarket:mainfrom
DukeDeSouth:fix/decimal-precision-rounding
Open

fix: use Decimal.quantize for base-10 rounding precision#291
DukeDeSouth wants to merge 2 commits into
Polymarket:mainfrom
DukeDeSouth:fix/decimal-precision-rounding

Conversation

@DukeDeSouth
Copy link
Copy Markdown

@DukeDeSouth DukeDeSouth commented Mar 2, 2026

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 with Decimal(str(x)).quantize(...) in round_down, round_normal, and round_up.

Simplifies to_token_decimals to convert to 6-decimal token units via Decimal multiplication and ROUND_HALF_EVEN quantization, removing the prior decimal_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.

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
@DukeDeSouth DukeDeSouth requested a review from a team as a code owner March 2, 2026 20:31
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread py_clob_client/order_builder/helpers.py Outdated
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected behavior in round_down function

1 participant