mirror of
https://github.com/golang/go
synced 2024-11-26 14:56:47 -07:00
go/constant: MakeFloat64(0) must return a value of Float kind
Fixes #42641. Change-Id: I10fdc7c90054b37ab5b303999015262691c12927 Reviewed-on: https://go-review.googlesource.com/c/go/+/273126 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
1308f11897
commit
750b3729dc
@ -370,16 +370,13 @@ func MakeUint64(x uint64) Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MakeFloat64 returns the Float value for x.
|
// MakeFloat64 returns the Float value for x.
|
||||||
|
// If x is -0.0, the result is 0.0.
|
||||||
// If x is not finite, the result is an Unknown.
|
// If x is not finite, the result is an Unknown.
|
||||||
func MakeFloat64(x float64) Value {
|
func MakeFloat64(x float64) Value {
|
||||||
if math.IsInf(x, 0) || math.IsNaN(x) {
|
if math.IsInf(x, 0) || math.IsNaN(x) {
|
||||||
return unknownVal{}
|
return unknownVal{}
|
||||||
}
|
}
|
||||||
// convert -0 to 0
|
return ratVal{newRat().SetFloat64(x + 0)} // convert -0 to 0
|
||||||
if x == 0 {
|
|
||||||
return int64Val(0)
|
|
||||||
}
|
|
||||||
return ratVal{newRat().SetFloat64(x)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeFromLiteral returns the corresponding integer, floating-point,
|
// MakeFromLiteral returns the corresponding integer, floating-point,
|
||||||
|
@ -7,6 +7,7 @@ package constant
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -620,6 +621,42 @@ func TestUnknown(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMakeFloat64(t *testing.T) {
|
||||||
|
var zero float64
|
||||||
|
for _, arg := range []float64{
|
||||||
|
-math.MaxFloat32,
|
||||||
|
-10,
|
||||||
|
-0.5,
|
||||||
|
-zero,
|
||||||
|
zero,
|
||||||
|
1,
|
||||||
|
10,
|
||||||
|
123456789.87654321e-23,
|
||||||
|
1e10,
|
||||||
|
math.MaxFloat64,
|
||||||
|
} {
|
||||||
|
val := MakeFloat64(arg)
|
||||||
|
if val.Kind() != Float {
|
||||||
|
t.Errorf("%v: got kind = %d; want %d", arg, val.Kind(), Float)
|
||||||
|
}
|
||||||
|
|
||||||
|
// -0.0 is mapped to 0.0
|
||||||
|
got, exact := Float64Val(val)
|
||||||
|
if !exact || math.Float64bits(got) != math.Float64bits(arg+0) {
|
||||||
|
t.Errorf("%v: got %v (exact = %v)", arg, got, exact)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// infinity
|
||||||
|
for sign := range []int{-1, 1} {
|
||||||
|
arg := math.Inf(sign)
|
||||||
|
val := MakeFloat64(arg)
|
||||||
|
if val.Kind() != Unknown {
|
||||||
|
t.Errorf("%v: got kind = %d; want %d", arg, val.Kind(), Unknown)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type makeTestCase struct {
|
type makeTestCase struct {
|
||||||
kind Kind
|
kind Kind
|
||||||
arg, want interface{}
|
arg, want interface{}
|
||||||
|
Loading…
Reference in New Issue
Block a user