Skip to content

Commit

Permalink
cue: add support for zero values in Value.Float64
Browse files Browse the repository at this point in the history
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 51fb5b8.

Signed-off-by: Kevin Burge <kcburge@pm.me>
Change-Id: I16a9fde39c36c519501c6cb3f87470a7f0cbab36
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/552142
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
kcburge authored and mvdan committed Apr 4, 2023
1 parent 752b8e4 commit 8d31dad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 11 additions & 9 deletions pkg/strconv/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"

0 comments on commit 8d31dad

Please sign in to comment.