google / go-jsonnet

Apache License 2.0
1.63k stars 235 forks source link

Decimal number displayed differently #718

Open Duologic opened 1 year ago

Duologic commented 1 year ago

Downstream issue: https://github.com/CertainLach/jrsonnet/issues/108

Decimal numbers are displayed differently depending on the implementation.

➜ jsonnet -e 0.97
0.96999999999999997

➜ jrsonnet -e 0.97
0.97

Further investigation also showed it depends on which functions used for representation:

# go jsonnet
❯ nix run nixpkgs#go-jsonnet -- -e "std.manifestJson(0.97)"
"0.97"
❯ nix run nixpkgs#go-jsonnet -- -e "std.toString(0.97)"
"0.96999999999999997"
❯ nix run nixpkgs#go-jsonnet -- -e 0.97
0.96999999999999997

# c++ jsonnet
❯ nix run nixpkgs#jsonnet -- -e "std.manifestJson(0.97)"
"0.96999999999999997"
❯ nix run nixpkgs#jsonnet -- -e "std.toString(0.97)"
"0.96999999999999997"
❯ nix run nixpkgs#jsonnet -- -e 0.97
0.96999999999999997

# rust jrsonnet
❯ cargo run -- -e "std.manifestJson(0.97)"
"0.97"
❯ cargo run -- -e "std.toString(0.97)"
"0.97"
❯ cargo run -- -e 0.97
0.97

@sparkprime suggest using the short version in Go:

My preference would be for the shortest exact string. There was no way to do that in C++ and we matched that behaviour in Go. Given we're trying to move people off of C++ to Go it may be a good time to switch Go over to the shorter output.

Initially I've only encountered this when using jrsonnet directly but we are working on a feature in Tanka to use the rust version alongside the Go version and this causes a massive diff on our code base. It would be nice if we could get this fixed in the Go version so we can get rid of the diff and keep our sanity.