douxun2023 2014-06-22 18:37
浏览 77
已采纳

为什么Go中有int而不是float?

In Go, there's the type int which may be equivalent to int32 or int64 depending on the system architecture. I can declare an integer variable without worrying about its size with:

var x int

Why isn't there the type float, which would be equivalent to float32 or float64 depending on my system's architecture? I wish I could also do:

var x float
  • 写回答

2条回答 默认 最新

  • douao8204 2014-06-22 18:44
    关注

    float were removed in the release 2011/01/20.

    You can still use a short variable declaration:

    x := 0.
    

    But as mentioned in the GO FAQ:

    For reasons of portability, we decided to make things clear and straightforward at the cost of some explicit conversions in the code.


    You can see the debate before 2011 in this thread:

    I'm a little dismayed even to see the suggestion of getting rid of the unsized float and complex types.
    People haven't had to really deal with this problem for a generation (a human generation, not a computer generation; the > early 90s was the last time this was really an issue), but this is exactly the moment in time when I think it's becoming relevant again.
    Between the transition to 64-bit chips and the transition to non-Intel based platforms (mobile chips, GPUs, etc), I think it's a huge mistake to take out these types.

    The problem with the analogy between integer types and float types is that:

    • in the case of integer types you don't care about the size unless it overflows.
    • In the case of float types, you always need to care about the size, because it always affects your answer (unless you're only doing arithmetic involving small integers * 2^n, in which case it's exact, in which case you'd be better off with a fixed-point representation).
      So there isn't the same possibility of "I just want a good representation".

    There has never been a speed advantage to 32-bit floats except in terms of memory use (and cache), so the existing 32-bit float type isn't defined as a "fast" float. It's just there (I presume) because that's what it's called in C. I wouldn't object to that if the float64 were called "double", which it is in most languages I know.

    But I really think the language would be nicer without the "float" type.
    Size really does matter for any floating-point use, either because of memory consumption or because of required precision.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器