为什么有int,但不是在Go中浮动? [英] Why is there int but not float in Go?

查看:161
本文介绍了为什么有int,但不是在Go中浮动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Go中,有 int 类型可能相当于 int32 int64 取决于系统架构。

  var x int 

为什么没有类型 float ,这相当于 float32 float64 取决于我的系统架构?我希望我也可以这样做:

  var x float 
release 2011/01/20 。


您仍然可以使用

  x:= 0. 
code>

但正如 GO FAQ
$ b


出于可移植性的原因,我们决定以代码中的某些显式转换为代价,让事情变得清晰明了。






你可以看到 2011年之前在这个帖子



I m是一点点沮丧,甚至看到摆脱无胶浮子和复杂类型点击
的人还没有真正解决这个问题一代(人的产生,不是计算机生成的建议。 > 90年代初是最后一次这是真正的问题),但是这正是我认为它正在变得相关的时刻。

在转换到64位芯片和过渡到非基于英特尔的平台
(移动芯片,GPU等),我认为这是一个巨大的错误,拿出这些类型。

整数类型和浮点类型之间的类比问题是:


  • 不关心大小,除非溢出

  • 在浮点类型的情况下,你总是需要关心大小,因为它总是影响你的答案(除非你只做小算术* 2 ^ n ,在这种情况下是确切的,在这种情况下,您最好用定点表示法)。
    So 不存在我只是想要一个很好的代表



  • 从来没有一个速度优势,除了内存使用(和高速缓存)之外,所以现有的32位浮点类型没有被定义为快速浮点。它只是在那里(我假设),因为这就是它所谓的C.我不会反对,如果
    float64被称为双,这是我所知道的大多数语言。



    但是我真的认为没有浮点类型的语言会更好。

    对于任何浮点使用来说,所需的精确度。



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

解决方案

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.

这篇关于为什么有int,但不是在Go中浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆