fix: goexports performs correct conversion on float32 constants
This commit is contained in:
committed by
Traefiker Bot
parent
15686873e0
commit
75a696a5c8
13
_test/math1.go
Normal file
13
_test/math1.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "math"
|
||||
|
||||
func main() {
|
||||
var f float32
|
||||
if f < math.MaxFloat32 {
|
||||
println("ok")
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// ok
|
||||
@@ -254,7 +254,14 @@ func genContent(dest, pkgName, license string) ([]byte, error) {
|
||||
|
||||
// fixConst checks untyped constant value, converting it if necessary to avoid overflow
|
||||
func fixConst(name string, val constant.Value) string {
|
||||
if val.Kind() == constant.Int {
|
||||
switch val.Kind() {
|
||||
case constant.Float:
|
||||
str := val.ExactString()
|
||||
if _, err := strconv.ParseFloat(str, 32); err == nil {
|
||||
return "float32(" + name + ")"
|
||||
}
|
||||
return name
|
||||
case constant.Int:
|
||||
str := val.ExactString()
|
||||
i, err := strconv.ParseInt(str, 0, 64)
|
||||
if err == nil {
|
||||
@@ -271,8 +278,10 @@ func fixConst(name string, val constant.Value) string {
|
||||
if err == nil {
|
||||
return "uint64(" + name + ")"
|
||||
}
|
||||
return name
|
||||
default:
|
||||
return name
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// genLicense generates the correct LICENSE header text from the provided
|
||||
|
||||
@@ -61,7 +61,7 @@ func init() {
|
||||
"Log2E": reflect.ValueOf(math.Log2E),
|
||||
"Logb": reflect.ValueOf(math.Logb),
|
||||
"Max": reflect.ValueOf(math.Max),
|
||||
"MaxFloat32": reflect.ValueOf(math.MaxFloat32),
|
||||
"MaxFloat32": reflect.ValueOf(float32(math.MaxFloat32)),
|
||||
"MaxFloat64": reflect.ValueOf(math.MaxFloat64),
|
||||
"MaxInt16": reflect.ValueOf(math.MaxInt16),
|
||||
"MaxInt32": reflect.ValueOf(math.MaxInt32),
|
||||
|
||||
@@ -61,7 +61,7 @@ func init() {
|
||||
"Log2E": reflect.ValueOf(math.Log2E),
|
||||
"Logb": reflect.ValueOf(math.Logb),
|
||||
"Max": reflect.ValueOf(math.Max),
|
||||
"MaxFloat32": reflect.ValueOf(math.MaxFloat32),
|
||||
"MaxFloat32": reflect.ValueOf(float32(math.MaxFloat32)),
|
||||
"MaxFloat64": reflect.ValueOf(math.MaxFloat64),
|
||||
"MaxInt16": reflect.ValueOf(math.MaxInt16),
|
||||
"MaxInt32": reflect.ValueOf(math.MaxInt32),
|
||||
|
||||
Reference in New Issue
Block a user