Why is there int but not float in Go?

2020-07-06 07:33发布

问题:

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:

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:

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.