I meet a very strange bug in golang 1.9.2: When I try to write int64(1.1*float64(time.Minute))
shows an error. The compiler said that the constant truncated to integer.
But when I change 1.1 to other float like 1.2 0.5 1.7, it compiles!
And it can also compile when I write it like:
value:=1.1*float64(time.Minute)
fmt.Println(int64(value))
Is it some bug of go itself? I run go on ubuntu14.04 x64
The constant 1.1*float64(time.Minute)
has a fractional part (the value is approximately 6.600000000000001e+10).
The specification says this about constant expressions :
The values of typed constants must always be accurately representable by values of the constant type.
The constant expression int64(1.1*float64(time.Minute))
fails to compile because an int64
cannot represent the number because it has a fractional part.
The values 1.2 * float64(time.Minute)
, 0.5 * float64(time.Minute)
and 1.7 * float64(time.Minute)
can be converted to an int64
in a constant expression because these values do not have a fractional part.
The following snippet compiles because the conversion from the variable value
to int64
is not a constant expression.
value:=1.1*float64(time.Minute)
fmt.Println(int64(value))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.