Summary
%g format specifier produces incorrect output for small decimal numbers. The precision parameter (number of significant digits) is not properly applied when the number has leading zeros after the decimal point.
Reproduction
$ go-jsonnet -e '"%.3g" % 0.001234'
"0"
$ go-jsonnet -e '"%.4g" % 0.001234'
"0.001"
$ go-jsonnet -e '"%g" % 0.001234'
"0.00123"
Expected (Python reference)
>>> "%.3g" % 0.001234
'0.00123'
>>> "%.4g" % 0.001234
'0.001234'
>>> "%g" % 0.001234
'0.001234'
Analysis
The issue appears to be in the %g format implementation when the number is in the fixed-point range (exponent >= -4). The precision specifies significant digits, but the implementation seems to not correctly account for leading zeros when calculating the number of fractional digits needed.
For example, 0.001234 has exponent -3, which is >= -4 (fixed-point range). With precision 3, the output should have 3 significant digits: 0.00123 (1.23 × 10⁻³). Instead, go-jsonnet outputs "0".
The same issue affects %#g (alternate flag) with small numbers:
$ go-jsonnet -e '"%#g" % 0.0001'
"0.00010"
Expected: "0.000100000" (6 significant digits with trailing zeros preserved)
Version
go-jsonnet v0.22.0
Summary
%gformat specifier produces incorrect output for small decimal numbers. The precision parameter (number of significant digits) is not properly applied when the number has leading zeros after the decimal point.Reproduction
Expected (Python reference)
Analysis
The issue appears to be in the
%gformat implementation when the number is in the fixed-point range (exponent >= -4). The precision specifies significant digits, but the implementation seems to not correctly account for leading zeros when calculating the number of fractional digits needed.For example,
0.001234has exponent -3, which is >= -4 (fixed-point range). With precision 3, the output should have 3 significant digits:0.00123(1.23 × 10⁻³). Instead, go-jsonnet outputs"0".The same issue affects
%#g(alternate flag) with small numbers:Expected:
"0.000100000"(6 significant digits with trailing zeros preserved)Version
go-jsonnet v0.22.0