Skip to content

Commit

Permalink
topdown/units_parse: Avoid extra decimal places for integers. (#5011)
Browse files Browse the repository at this point in the history
This commit cleans up the output of `units.parse` for integer values. Previously, all values from `units.parse` had their precision set to 10 places past the decimal point, regardless of whether that much precision was actually needed. This commit removes the extra decimal places for integers, giving cleaner outputs.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
  • Loading branch information
philipaconrad authored Aug 12, 2022
1 parent 295d251 commit 39fa5cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/cases/testdata/units/test-units-precision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
cases:
- data:
modules:
- |
package test
p {
json.marshal(units.parse("1G")) == json.marshal(1000000000)
}
note: units_parse/no decimal places for integers
query: data.test.p = x
want_result:
- x: true
5 changes: 5 additions & 0 deletions topdown/parse_units.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func builtinUnits(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) e

numRat.Mul(numRat, &x)

// Cleaner printout when we have a pure integer value.
if numRat.IsInt() {
return iter(ast.NumberTerm(json.Number(numRat.Num().String())))
}

// When using just big.Float, we had floating-point precision
// issues because quantities like 0.001 are not exactly representable.
// Rationals (such as big.Rat) do not suffer this problem, but are
Expand Down

0 comments on commit 39fa5cf

Please sign in to comment.