From 8d31dad6f551bedda23f1c9fdf1966bc86dec986 Mon Sep 17 00:00:00 2001 From: Kevin Burge Date: Tue, 26 Apr 2022 18:36:02 -0500 Subject: [PATCH] cue: add support for zero values in Value.Float64 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix the error when attempting to use strconv.FormatFloat with a zero value: cannot use 0.0 (type float) as float64 in argument 0 to strconv.FormatFloat: value was rounded down The tests for maximum and minimum in ValueFloat64 do not allow for zero. The below check incorrectly sees zero as smaller, because the Sign of zero (0) is less than the Sign of smallestPosFloat64 (1). So, it was clear this logic was not accounting for zero. if n.X.Cmp(smallestPosFloat64) == -1 { Fixes #1669. Closes #1670 as merged as of commit 51fb5b80. Signed-off-by: Kevin Burge Change-Id: I16a9fde39c36c519501c6cb3f87470a7f0cbab36 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/552142 Reviewed-by: Daniel Martí Unity-Result: CUEcueckoo TryBot-Result: CUEcueckoo Reviewed-by: Roger Peppe --- cue/types.go | 3 +++ cue/types_test.go | 8 ++++++++ pkg/strconv/testdata/gen.txtar | 20 +++++++++++--------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/cue/types.go b/cue/types.go index fbf8ecbb735..afc5beebb39 100644 --- a/cue/types.go +++ b/cue/types.go @@ -528,6 +528,9 @@ func (v Value) Float64() (float64, error) { if err != nil { return 0, err } + if n.X.IsZero() { + return 0.0, nil + } if n.X.Negative { if n.X.Cmp(smallestNegFloat64) == 1 { return -0, ErrAbove diff --git a/cue/types_test.go b/cue/types_test.go index fb8759a0526..0f27690d5da 100644 --- a/cue/types_test.go +++ b/cue/types_test.go @@ -459,6 +459,14 @@ func TestFloat(t *testing.T) { float64: -1, fmt: 'g', kind: IntKind, + }, { + value: "0.0", + float: "0.0", + mant: "0", + exp: -1, + float64: 0.0, + fmt: 'g', + kind: FloatKind, }, { value: "1.0", float: "1.0", diff --git a/pkg/strconv/testdata/gen.txtar b/pkg/strconv/testdata/gen.txtar index 8eeea449979..1302e96cdb8 100644 --- a/pkg/strconv/testdata/gen.txtar +++ b/pkg/strconv/testdata/gen.txtar @@ -12,6 +12,7 @@ t6: strconv.FormatInt(170_141_183_460_469_231_731_687_303_715_884_105_728, 10) t7: strconv.FormatInt(64, 16) t8: strconv.FormatUint(170_141_183_460_469_231_731_687_303_715_884_105_728, 10) t9: strconv.FormatUint(61, 62) +t10: strconv.FormatFloat(0.0, 102, -1, 64) -- out/strconv -- Errors: t2: int 300 overflows byte in argument 1 in call to strconv.FormatFloat: @@ -23,13 +24,14 @@ t4: cannot use 1.0 (type float) as int in argument 2 to strconv.FormatFloat: ./in.cue:6:31 Result: -t1: "40" -t2: _|_ // t2: int 300 overflows byte in argument 1 in call to strconv.FormatFloat -t3: _|_ // t3: cannot use -1 (type int) as byte in argument 1 to strconv.FormatFloat -t4: _|_ // t4: cannot use 1.0 (type float) as int in argument 2 to strconv.FormatFloat -t5: "true" -t6: "170141183460469231731687303715884105728" -t7: "40" -t8: "170141183460469231731687303715884105728" -t9: "Z" +t1: "40" +t2: _|_ // t2: int 300 overflows byte in argument 1 in call to strconv.FormatFloat +t3: _|_ // t3: cannot use -1 (type int) as byte in argument 1 to strconv.FormatFloat +t4: _|_ // t4: cannot use 1.0 (type float) as int in argument 2 to strconv.FormatFloat +t5: "true" +t6: "170141183460469231731687303715884105728" +t7: "40" +t8: "170141183460469231731687303715884105728" +t9: "Z" +t10: "0"