douxuqiao6394 2017-11-30 07:41
浏览 78

为什么当int64(0.5 * float64(time.Minute))不在golang 1.9.2中时,int64(1.1 * float64(time.Minute))是错误?

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

  • 写回答

1条回答 默认 最新

  • douxiawei9318 2019-09-11 15:28
    关注

    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))
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP