Skip to content

Fix floating point precision in dollar-to-cents conversion#257

Closed
masonsfc wants to merge 2 commits intomainfrom
fix/floating-point-price-conversion
Closed

Fix floating point precision in dollar-to-cents conversion#257
masonsfc wants to merge 2 commits intomainfrom
fix/floating-point-price-conversion

Conversation

@masonsfc
Copy link
Copy Markdown

Summary

  • Fix IEEE 754 floating point precision error in price conversion that breaks node creation
  • $17.60 * 100 = 1760.0000000000002 which the API rejects as "invalid type: floating point, expected i64"

Changes

  • priceWholeToCents(): wrap both number and string paths with Math.round()
  • dollarsToCents(): change Math.ceil to Math.round (ceil also overcharges by 1 cent on edge cases)
  • Add 3 regression tests covering known problematic prices

Reproducer

sf nodes create test -n 1 --any-zone --max-price 17.60 --auto
# Error: Failed to deserialize the JSON body: max_price_per_node_hour: 
# invalid type: floating point `1760.0000000000002`, expected i64

Test

npx vitest run src/helpers/test/units.test.ts
# 5 passed

Reported by Ryan Tremblay — confirmed broken in 0.31.x, works in 0.29.1.

$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.
@semanticdiff-com
Copy link
Copy Markdown

semanticdiff-com bot commented Mar 25, 2026

Review changes with  SemanticDiff

Changed Files
File Status
  src/helpers/units.ts  61% smaller
  src/helpers/test/units.test.ts  0% smaller

@masonsfc masonsfc closed this Mar 25, 2026
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.

1 participant