Skip to content

chore: exclude Python cache files and refactor math_scaler precision …#443

Merged
Sadeequ merged 1 commit into
StellarFlow-Network:mainfrom
AgilityB:Scaling-for-Fixed-10-7-Normalizations
Jun 2, 2026
Merged

chore: exclude Python cache files and refactor math_scaler precision …#443
Sadeequ merged 1 commit into
StellarFlow-Network:mainfrom
AgilityB:Scaling-for-Fixed-10-7-Normalizations

Conversation

@AgilityB

@AgilityB AgilityB commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

fix(analytics): replace float arithmetic with Decimal scaling in math_scaler

What Rewrote
math_scaler.py
to use Python's decimal.Decimal library for all rate scaling operations targeting the Soroban smart contract layer.

Why Binary float multiplication (math.floor(value * factor)) silently introduces sub-unit rounding errors. For example, 0.1 * 10_000_000 in native float yields 999999.9999... which floors to 999999 — one unit short. Soroban contract logic expects strict deterministic integers at a fixed 10^7 base, so even a single-unit deviation can invalidate payload verification.

How

All inputs are coerced via Decimal(str(value)) before any arithmetic, bypassing the binary-float approximation at the source
Truncation uses ROUND_DOWN (toward zero) for platform-independent, bit-identical results
scale_down now returns Decimal instead of float to prevent downstream callers from reintroducing imprecision
Added pack_rate(value) -> int as the explicit canonical entry point for Soroban payload packing
Changes

scale_up — Decimal-based multiply + ROUND_DOWN truncation
scale_down — returns Decimal instead of float
multiply_rates / cross_feed_multiply / floor_divide — unchanged in logic, now benefit from fixed scale_up
pack_rate — new SCALE_7 packing entry point
Testing Validated with inline assertions covering known float traps (0.1, 1.1, 0.00065), cross-feed multiplication, scale_down type guarantees, and floor_divide correctness.

Closes #337

…handling

- Remove Python 3.13 bytecode cache files from staging (__pycache__)
- Refactor scale_up() to use Decimal arithmetic with ROUND_DOWN for deterministic cross-platform results
- Replace float division with Decimal in scale_down() to preserve precision in downstream operations
- Add _to_decimal() helper for safe numeric coercion via string representation, preventing binary-float approximation errors
- Update multiply_rates() and cross_feed_multiply() signatures to accept Union[int, float, Decimal]
- Enhance docstrings with detailed parameter/return documentation and clarify 10^7/10^14 precision semantics
- Add comprehensive comments and type hints for maintainability
- Ensures bit-identical results regardless of Python environment or CPU architecture
@drips-wave

drips-wave Bot commented Jun 2, 2026

Copy link
Copy Markdown

@AgilityB Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Sadeequ Sadeequ merged commit 868df92 into StellarFlow-Network:main Jun 2, 2026
1 check failed
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.

💸 Math-Precision | Bit-Shift Scaling for Fixed 10^7 Normalizations

2 participants