google / google/jsonnet

std.format %e/%g do not follow documented Python rounding semantics

Open
#1,327 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Jsonnet
Stars
7.6k
Forks
475
PR merge metrics
No merged PRs in 30d

Description

`std.format` is documented as following Python formatting rules:
https://jsonnet.org/ref/stdlib.html#std-format

In jsonnet v0.22.0, `%e`/`%g` do not renormalize the mantissa when rounding carries from `9...` to `10...`. `%g` precision `0` also appears to be passed through as a negative fractional precision, while Python treats precision `0` for `%g` as precision `1`.

Repro table:

| Expression | Python 3 | C++ jsonnet v0.22.0 | go-jsonnet v0.22.0 | jrsonnet v0.5.0-pre99 |
| --- | --- | --- | --- | --- |
| `"%.0g" % 1.0` | `"1"` | `"5e+00"` | `"5e+00"` | `"0e+00"` |
| `"%+.0g" % 1.0` | `"+1"` | `"+5e+00"` | `"+5e+00"` | `"+0e+00"` |
| `"%.0g" % 0.1` | `"0.1"` | `"5"` | `"5"` | `"0"` |
| `"%.1g" % 9.9` | `"1e+01"` | `"10"` | `"10"` | `"10"` |
| `"%.2g" % 99.9` | `"1e+02"` | `"100"` | `"100"` | `"100"` |
| `"%.0e" % 9.5e10` | `"1e+11"` | `"10e+10"` | `"10e+10"` | `"10e+10"` |
| `"%.1e" % 9.99` | `"1.0e+01"` | `"10.0e+00"` | `"10.0e+00"` | `"10.0e+00"` |

Commands used:

```bash
python3 - <

Contributor guide

Open the contributing guide

Research direction

Start in stdlib/std.jsonnet, where %g calls render_float_dec/render_float_sci, and run the Python and jsonnet reproduction commands from the issue. Done means %e and %g match the documented Python results, including mantissa renormalization after rounding and treating %g precision 0 as precision 1.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.