https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
The germane part:
Since integer types use two's complement arithmetic, you can infer the min/max constant values for
intanduint. For example,const MaxUint = ^uint(0) const MinUint = 0 const MaxInt = int(MaxUint >> 1) const MinInt = -MaxInt - 1
As per @CarelZA's comment:
uint8 : 0 to 255
uint16 : 0 to 65535
uint32 : 0 to 4294967295
uint64 : 0 to 18446744073709551615
int8 : -128 to 127
int16 : -32768 to 32767
int32 : -2147483648 to 2147483647
int64 : -9223372036854775808 to 9223372036854775807
Answer from nmichaels on Stack Overflowhttps://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
The germane part:
Since integer types use two's complement arithmetic, you can infer the min/max constant values for
intanduint. For example,const MaxUint = ^uint(0) const MinUint = 0 const MaxInt = int(MaxUint >> 1) const MinInt = -MaxInt - 1
As per @CarelZA's comment:
uint8 : 0 to 255
uint16 : 0 to 65535
uint32 : 0 to 4294967295
uint64 : 0 to 18446744073709551615
int8 : -128 to 127
int16 : -32768 to 32767
int32 : -2147483648 to 2147483647
int64 : -9223372036854775808 to 9223372036854775807
I would use math package for getting the maximal and minimal values for integers:
package main
import (
"fmt"
"math"
)
func main() {
// integer max
fmt.Printf("max int64 = %+v\n", math.MaxInt64)
fmt.Printf("max int32 = %+v\n", math.MaxInt32)
fmt.Printf("max int16 = %+v\n", math.MaxInt16)
// integer min
fmt.Printf("min int64 = %+v\n", math.MinInt64)
fmt.Printf("min int32 = %+v\n", math.MinInt32)
fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
fmt.Printf("max float32 = %+v\n", math.MaxFloat32)
// etc you can see more int the `math`package
}
Output:
max int64 = 9223372036854775807
max int32 = 2147483647
max int16 = 32767
min int64 = -9223372036854775808
min int32 = -2147483648
max float64 = 1.7976931348623157e+308
max float32 = 3.4028234663852886e+38
Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
Using minimum and maximum int64 limits leads to error
go - Get maximum int64 value using 2's complement arithmetic golang - Stack Overflow
proposal: math: MinOf and MaxOf for generic numeric limits
Today I was trying to write a generic function to return the maximum value for floats, ints and uints. It seems straightforward, right? It's not!
The function signature:
type Types interface {
~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint |
~int | ~int64 | ~int32 | ~int16 | ~int8 |
~float64 | ~float32
}
func infFor[T Types]() TMy first thought was to write a switch checking the type, like this:
func infFor[T Types]() T {
var v T
switch any(v).T {
case float64:
return T(math.Inf(1))
case int8:
return T(math.MaxInt8)
...
}
}
But this doesn't work for user defined types (type myInt int).
Then I tried to split the function in two parts, check if T is a float or (u)int, this makes the job easier.
func infFor[T Types]() T {
// Check if T is a float.
var f float64 = 1.5
if float64(T(f)) == f {
return T(math.Inf(1))
}
// Handle (u)ints ...
}Converting 1.5 to an integer type will truncate the value and then float64(T(f)) == f is false.
The value 1.5 is important because it can be represented both by float64 and float32, 1.1 can't.
After the check we know the value is an integer, but the compiler doesn't, so we can't use bit arithmetic. The solution I found is to check when the value overflows.
This is the final version:
func infFor[T Types]() T {
// Check if T is a float.
var f float64 = 1.5
if float64(T(f)) == f {
return T(math.Inf(1))
}
maxValues := [...]uint64{
math.MaxInt8,
math.MaxUint8,
math.MaxInt16,
math.MaxUint16,
math.MaxInt32,
math.MaxUint32,
math.MaxInt64,
math.MaxUint64,
}
var v T
// Check when v overflows.
for i := 0; v+1 > 0; i++ {
v = T(maxValues[i])
}
return v
}