You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A constant value x can be converted to type T in any of these cases:
x is representable by a value of type T.
The Go compiler allows conversion in certain cases, where an integer constant value is not representable as a value of type float32 as in:
package main
import"fmt"funcmain() {
// badconstA=0x01ffffffconstB=float32(A)
fmt.Printf("%d\n%f\n", A, B)
// okconstC=0x01fffffeconstD=float32(C)
fmt.Printf("%d\n%f\n", C, D)
// okconstE=0x00ffffffconstF=float32(E)
fmt.Printf("%d\n%f\n", E, F)
}
I suppose there are similar cases when converting an integer constant into float64, complex64, or complex128 type.
The text was updated successfully, but these errors were encountered:
ianlancetaylor
changed the title
cmd/compile: Incorrect constant conversion
spec: does not permit imprecise conversion of non-floating-point constant to floating point type
Dec 31, 2015
Agreeing with @ianlancetaylor. This is a grey area that should be spec-ed out more precisely. (Some) rounding is acceptable when converting to floats.
griesemer
changed the title
spec: does not permit imprecise conversion of non-floating-point constant to floating point type
spec: clarify imprecise conversions of non-floating-point constant to floating point type
Jan 4, 2016
... or perhaps the spec is does not convey the intention.
The spec says (https://golang.org/ref/spec#Conversions)
The Go compiler allows conversion in certain cases, where an integer constant value is not representable as a value of type
float32
as in:I suppose there are similar cases when converting an integer constant into
float64
,complex64
, orcomplex128
type.The text was updated successfully, but these errors were encountered: