Skip to content

elderapo/vite-float-precision-bug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vite 8 JSON float precision loss

Vite 8 (rolldown/oxc) truncates floating-point numbers when bundling JSON imports, changing their IEEE 754 value. Vite 7 (esbuild) preserves full precision.

The bug

When a JSON file contains numbers with 17 significant digits (the maximum for IEEE 754 double-precision), Vite 8 truncates them to 16 significant digits. This produces a different float value at runtime.

For example, 114.35143799257997 in data.json becomes 114.35143799257996 in the bundle - a difference of ~1.4e-14. While small, this matters for applications that depend on exact float equality (physics simulations, deterministic algorithms, cryptographic seeds, etc.).

Inline literals in .js files are not affected - only values coming from JSON imports.

Reproduction

# Install dependencies (Vite 8)
npm install

# Build and preview
npx vite build
npx vite preview
# Open browser - you'll see 4/8 FAIL

# Compare with Vite 7
npm install vite@7
npx vite build --outDir dist-v7
npx vite preview --outDir dist-v7
# Open browser - you'll see 8/8 PASS

How it works

src/data.json contains 8 floats with 17 significant digits. src/main.js imports them and compares each value against the same number written as an inline literal. If the bundler preserves full precision, all 8 match (===). If it truncates, some values change.

Results

Vite 7.3.1 (esbuild): 8/8 passed

[PASS] values[0]: json=138.64661140304366427 inline=138.64661140304366427
[PASS] values[1]: json=114.35143799257997443 inline=114.35143799257997443
[PASS] values[2]: json=104.61834129161124451 inline=104.61834129161124451
[PASS] values[3]: json=120.46208811014057005 inline=120.46208811014057005
[PASS] values[4]: json=406.31486713248995102 inline=406.31486713248995102
[PASS] values[5]: json=333.97189575898084968 inline=333.97189575898084968
[PASS] values[6]: json=465.65202119092083421 inline=465.65202119092083421
[PASS] values[7]: json=206.87910286098676238 inline=206.87910286098676238

Vite 8.0.1 (rolldown/oxc): 4/8 passed

[PASS] values[0]: json=138.64661140304366427 inline=138.64661140304366427
[FAIL] values[1]: json=114.35143799257996022 inline=114.35143799257997443 diff=-1.42e-14
[PASS] values[2]: json=104.61834129161124451 inline=104.61834129161124451
[FAIL] values[3]: json=120.46208811014055584 inline=120.46208811014057005 diff=-1.42e-14
[FAIL] values[4]: json=406.31486713248989417 inline=406.31486713248995102 diff=-5.68e-14
[PASS] values[5]: json=333.97189575898084968 inline=333.97189575898084968
[FAIL] values[6]: json=465.65202119092077737 inline=465.65202119092083421 diff=-5.68e-14
[PASS] values[7]: json=206.87910286098676238 inline=206.87910286098676238

Environment

  • Node.js v22
  • Vite 7.3.1 vs Vite 8.0.1
  • Linux x86_64

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors