feat: BigNumber fixed-point decimal safety wrapper for multi-tariff scaling. - #83
Merged
elizabetheonoja-art merged 1 commit intoJun 25, 2026
Conversation
…caling (Utility-Protocol#53) Replaces Number-based arithmetic (which drifts up to 0.05% over aggregation windows) with an exact fixed-point Decimal layer over BigNumber.js. - utils/decimal.ts: branded Decimal<P> over a cloned BigNumber (28 sig digits, ROUND_HALF_UP); factory + add/sub/mul/div/min/max/sum/average/neg/abs and comparisons. Intermediates stay exact; overflow past MAX_SAFE (10^21) throws SafetyRangeError; unsupported precision throws PrecisionError. toRedux()/ fromRedux() give lossless string serialization; toDecimalNumber() warns on precision loss at the charting boundary - types/meter.ts: MeterReading/ResourceConsumption/TariffRate keyed to the Decimal type, with canonical precision per resource (water 0, cost 2, electricity 3, gas 4, submeter 6, nano 9) - utils/aggregation.ts: sum/average/min/max/cumulative/windowed totals and applyTariff, all Decimal end-to-end (never cast back to number) - store/slices/resourceSlice.ts: store keeps readings string-encoded (SerializedDecimal) and rehydrates Decimals only in selectors - components/dashboard/ResourceChart.tsx: SVG series that converts via toDecimalNumber at render while keeping the headline total exact - hooks/useResourceData.ts: converts API string decimals to Decimal at the resource precision on ingestion - tests: mixed-precision add upcast, multiplication overflow, serialization round-trip, and 1000-iteration zero-drift accumulation
elizabetheonoja-art
approved these changes
Jun 25, 2026
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.
BigNumber Fixed-Point Arithmetic Safety Wrapper for Multi-Tariff Decimal Scaling (#53)
Closes #53
What's included
src/utils/decimal.tsDecimal<P>over a cloned BigNumber (28 sig digits, ROUND_HALF_UP — no global-config mutation). Factorydecimal(value, precision)+add/sub/mul/div/min/max/sum/average/neg/absandeq/gt/gte/lt/lte. Overflow pastMAX_SAFE(10²¹) throwsSafetyRangeError; unsupported precision / bad input throwsPrecisionError.toRedux()/fromRedux()give lossless string serialization;toDecimalNumber()warns on precision loss.src/types/meter.tsMeterReading/ResourceConsumption/TariffRatekeyed toDecimal, with canonical precision per resource (water 0, cost 2, electricity 3, gas 4, submeter 6, nano 9).src/utils/aggregation.tssumReadings,averageReadings,min/maxReading,cumulativeTotals,aggregateByWindow, andapplyTariff— Decimal end-to-end, never cast back tonumbermid-pipeline.src/store/slices/resourceSlice.tsSerializedDecimal(JSON/DevTools-safe) and rehydrates Decimals only inside selectors.src/components/dashboard/ResourceChart.tsxtoDecimalNumberonce at render, while the headline total stays exact (toFixed).src/hooks/useResourceData.tsDecimalat the resource's precision on ingestion (never parsed asnumber).tests/unit/{decimal,aggregation,resourceSlice}.test.ts