Commit 7149c61
committed
fix: correct %g format floating-point precision and star width rounding
Motivation:
The %g format operator had two precision-related bugs:
1. Floating-point imprecision in roundedGenericExponent: math.pow(10, -5)
returns 9.999999999999999E-6 instead of exact 1e-5, causing log10 to
return -4.3e-17 instead of 0. math.floor(-4.3e-17) incorrectly returns -1.
2. Star width rounding: width.asInt truncates (3.7 → 3) instead of rounding
(3.7 → 4), not matching go-jsonnet behavior.
Modification:
1. Changed math.floor(math.log10(x)) to math.floor(math.log10(x) + 1e-10)
in roundedGenericExponent (2 locations). The epsilon (1e-10) corrects
floating-point errors without breaking values close to powers of 10.
2. Changed width.asInt to Math.round(width.asDouble).toInt for star width
and star precision (3 locations).
3. Removed debug println statement from formatGeneric.
Result:
- %g and %#g now correctly handle values with negative exponents (0.1, 0.0001)
- Star width/precision arguments now round instead of truncate
- All existing tests pass, plus new comprehensive test coverage
- Matches go-jsonnet and Python % formatting behavior
References:
- Python % formatting documentation
- go-jsonnet v0.21.0 reference implementation1 parent 750dc7e commit 7149c61
3 files changed
Lines changed: 55 additions & 2 deletions
File tree
- sjsonnet
- src/sjsonnet
- test/resources/new_test_suite
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1060 | 1060 | | |
1061 | 1061 | | |
1062 | 1062 | | |
1063 | | - | |
| 1063 | + | |
1064 | 1064 | | |
1065 | 1065 | | |
1066 | | - | |
| 1066 | + | |
1067 | 1067 | | |
1068 | 1068 | | |
1069 | 1069 | | |
| |||
Lines changed: 52 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 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 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
0 commit comments