为什么Go语言中有int类型但没有float类型?

huangapple go评论77阅读模式
英文:

Why is there int but not float in Go?

问题

在Go语言中,有一种类型叫做int,它可能在系统架构上等同于int32int64。我可以声明一个整数变量而不用担心它的大小,例如:

var x int

为什么没有一种类型叫做float,它可以根据系统架构等同于float32float64呢?我希望我也可以这样做:

var x 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

答案1

得分: 13

浮点数在2011/01/20发布的版本中被移除了。

你仍然可以使用短变量声明

x := 0.

但正如在GO FAQ中提到的:

>出于可移植性的考虑,我们决定以一些代码中的显式转换为代价,使事情变得清晰明了。


你可以在2011年之前的这个讨论中看到争论:

>>我甚至对于去除无大小限制的浮点数和复数类型的建议感到有些沮丧。
人们已经有一代人(人类的一代,而不是计算机的一代;早在90年代初,这个问题就不再是一个问题了)没有真正处理过这个问题了,但我认为现在正是这个问题再次变得相关的时刻。
在过渡到64位芯片和过渡到非英特尔平台(移动芯片、GPU等)之间,
>我认为去除这些类型是一个巨大的错误。

>在整数类型和浮点类型之间进行类比的问题在于:

>- 对于整数类型,除非溢出,你不关心大小

  • 对于浮点类型,你总是需要关心大小,因为它总是会影响你的答案(除非你只进行涉及小整数* 2^n的算术运算,在这种情况下它是精确的,在这种情况下,你最好使用定点表示)。
    所以没有相同的可能性“我只想要一个好的表示”

>除了内存使用(和缓存)方面,32位浮点数从来没有速度优势,所以现有的32位浮点数类型并没有被定义为“快速”浮点数。它只是存在(我推测)因为在C语言中它被称为这样。如果float64被称为“double”,那我不会反对,因为大多数我知道的语言都是这样的。

>但我真的认为如果没有“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.

答案2

得分: 8

对于整数,通常希望有一个整数类型,其大小与平台的本机字大小相同:这样做有性能优势,并且对于与使用字大小的系统的其他部分进行低级别互操作性也有好处。

对于浮点数值,情况并非如此。即使在32位系统上,双精度浮点数(Go的float64)通常比单精度浮点数(float32)更常见,并且通常不会更慢。单精度浮点数运算相对较少见,通常只在内存使用或输入输出速度是更强烈的考虑因素时才有用。

因此,尽管您写道float“将等同于float32float64,具体取决于[您]系统的架构”,但我不确定您认为它应该等同于float32的架构是什么。

英文:

With integers, it is very common to want an integer type whose size is the platform's native word size: this has performance benefits, as well as benefits for low-level interoperability with other parts of the system that use the word size.

With floating-point values, this is not the case. Even on 32-bit systems, double-precision floating-point (Go's float64) is generally much more common, and generally not slower, than single-precision (float32). Single-precision floating-point arithmetic is relatively uncommon, and is generally only useful when memory usage or input-output speed is a much stronger consideration.

So although you write that float "would be equivalent to float32 or float64 depending on [your] system's architecture", I'm not sure on what architecture you feel it should be equivalent to float32.

huangapple
  • 本文由 发表于 2014年6月23日 02:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/24354439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定