Fix floating point precision in dollar-to-cents conversion#257
Closed
Fix floating point precision in dollar-to-cents conversion#257
Conversation
$17.60 * 100 = 1760.0000000000002 in IEEE 754, which the API rejects as "invalid type: floating point, expected i64" when deserializing max_price_per_node_hour. Fix: wrap all dollar * 100 conversions with Math.round() to ensure integer cents: - priceWholeToCents() number path - priceWholeToCents() string path - dollarsToCents() (was Math.ceil which also overcharges by 1 cent) Added 3 regression tests covering known problematic prices ($17.60, $12.50, $9.99, etc.) that verify cents are always integers. Fixes: "Failed to deserialize the JSON body" error when creating spot nodes with certain price values.
Changed Files
|
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.
Summary
$17.60 * 100 = 1760.0000000000002which the API rejects as "invalid type: floating point, expected i64"Changes
priceWholeToCents(): wrap both number and string paths withMath.round()dollarsToCents(): changeMath.ceiltoMath.round(ceil also overcharges by 1 cent on edge cases)Reproducer
Test
Reported by Ryan Tremblay — confirmed broken in 0.31.x, works in 0.29.1.