Commit 914793a
committed
Fix float precision loss in JSON serialization and cpool deduplication (issue #420)
Two root causes behind 'RUNTIME ERROR: Unknown LOADK index' on -c/-x path:
1. gravity_json.c: float constants were serialized with '%f' (6 decimal
places), silently rounding small values like -0.000000004 to -0.000000.
When two distinct floats rounded to the same string, one was dropped from
the JSON pool, leaving the bytecode referencing a non-existent index.
Fixed by switching to '%.17g' (17 significant digits, full IEEE 754
double round-trip), with a '.0' suffix appended for whole-number values
so they deserialize as float rather than integer.
2. gravity_value.c: gravity_function_cpool_add used the epsilon-based
gravity_value_equals (EPSILON=1e-6) to detect duplicate constants.
Any two floats differing by less than 1e-6 were merged into one cpool
entry, causing index mismatches at runtime. The cpool now uses exact
bit-level comparison (v.f != v2.f) for float values before falling back
to the fuzzy equality check used for all other types.
Also fix run_all.sh to work on macOS where GNU 'timeout' is not available:
detect 'timeout', 'gtimeout' (brew coreutils), or fall back to a pure-bash
background kill-watcher that returns exit code 124 on expiry.1 parent 93930ac commit 914793a
3 files changed
Lines changed: 47 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
593 | 593 | | |
594 | 594 | | |
595 | 595 | | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
596 | 604 | | |
597 | 605 | | |
598 | 606 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
302 | | - | |
303 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
304 | 314 | | |
305 | 315 | | |
306 | 316 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
13 | 39 | | |
14 | 40 | | |
15 | 41 | | |
| |||
24 | 50 | | |
25 | 51 | | |
26 | 52 | | |
27 | | - | |
| 53 | + | |
28 | 54 | | |
29 | 55 | | |
30 | 56 | | |
| |||
0 commit comments