Why is int but not floating in Go?

Go has an int type, which can 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 is there no type of float that is equivalent to float32 or float64 depending on my system architecture? I would also like:

 var x float 
+8
floating-point types int go
source share
2 answers

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

You can still use a short declaration of variables :

 x := 0. 

But as mentioned in GO FAQs :

For portability reasons, we decided to make everything clear and understandable due to some explicit transformations in the code.


You can see the discussion until 2011 in this thread :

I'm a little confused even to see the offer to get rid of a non-standard float and complex types.
People didnโ€™t really have to deal with this problem for generations (the human generation, not the computer generation, a> the beginning of the 90s is the last time it was really a problem), but this is exactly the moment when I think it becomes again relevant.
Between switching to 64-bit chips and switching to platforms other than Intel (mobile chips, GPUs, etc.), I think it's a huge mistake to take these types out.

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

  • in the case of integer types, you do not need a size if it does not overflow .
  • In the case of float types, you always need to take care of the size because it always affects your answer (unless you are doing arithmetic involving small integers * 2^n , in which case it is accurate, in which case you would be better off with fixed point representation).
    So there is no such possibility: "I just want a good performance . "

There has never been a speed advantage for 32-bit floats, with the exception of memory (and cache) usage, so the existing 32-bit float type is not defined as a โ€œfastโ€ float. It is simple (I suppose) because it is what he called in C. I would not mind it if float64 were called "double", which is in most languages โ€‹โ€‹that I know.

But I really think that the language will be more enjoyable without the "float" type.
Size really matters for any floating point use, either due to memory consumption or due to the required accuracy.

+11
source share

With integers, an integer type is often required, the size of which is the size of the native word of the platform: this has performance advantages, as well as advantages for low-level interaction 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 float64 ) is usually much more common and, as a rule, no slower than single point ( float32 ). Single-precision floating-point arithmetic is relatively unusual and is generally only useful when memory usage or I / O speed is much more important.

So, although you write that float "will be equivalent to float32 or float64 depending on [your] system architecture," I'm not sure which architecture, in your opinion, should be equivalent to float32 .

+6
source share

All Articles