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
To convert an integer data type to float you can wrap the integer with float64() or float32. Explanation: Firstly we declare a variable x of type int64 with a value of 5. Then we wrap x with float64(), which converts the integer 5 to float value of 5.00. The %.
int is one of the available numeric data types in Go . int has a platform-dependent size, as, on a 32-bit system, it holds a 32 bit signed integer, while on a 64-bit system, it holds a 64-bit signed integer.
So you cannot store a float value in an int object through simple assignment. You can store the bit pattern for a floating-point value in an int object if the int is at least as wide as the float , either by using memcpy or some kind of casting gymnastics (as other answers have shown).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With