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 Overflow Top answer 1 of 13
350
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
2 of 13
99
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
Go
go.dev › play › p › qeElniC-Nin
Go Playground - The Go Programming Language
Common problems companies solve with Go · Stories about how and why companies use Go
Using minimum and maximum int64 limits leads to error
Problem statement A tool I am using is generating the following schema, which sets minimum and maximum limits for a parameter of type integer: "schema": { "maximum": 92233720368... More on github.com
go - Get maximum int64 value using 2's complement arithmetic golang - Stack Overflow
How to get maximum int64 value using 2's complement arithmetic in golang More on stackoverflow.com
max int64 and uint64 handling
Problem statement Max int64 and uint64 seems to be wrong even if correct in the json schema definition go test -race ./... # github.com/opencontainers/image-spec/specsgo specsgo/int64_validator.go:... More on github.com
Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
Today I was trying to write a generic function to return the maximum value for floats, ints and uints. It seems… More on reddit.com
Reddit
reddit.com › r/golang › writing a function that returns the max value of a type [~float64 | ~uint | ~int t]
r/golang on Reddit: Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
March 17, 2024 - r/golang · • 2y ago · rschio · 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]() T ·
gosamples
gosamples.dev › tutorials › the maximum and minimum value of the int types in go
🍰 The maximum and minimum value of the int types in Go
April 14, 2022 - To get the maximum and minimum value of various integer types in Go, use the math package constants. For example, to get the minimum value of the int64 type, which is -9223372036854775808, use the math.MinInt64 constant. To get the maximum value of the int64 type, which is 9223372036854775807, ...
GitHub
github.com › go-swagger › go-swagger › issues › 2755
Using minimum and maximum int64 limits leads to error · Issue #2755 · go-swagger/go-swagger
April 11, 2022 - if err := validate.MinimumInt("limit", ...api_logs_parameters.go:95:60: cannot use -9.223372036854776e+18 (untyped float constant -9.22337e+18) as int64 value in argument to validate.MinimumInt (truncated) go/server/restapi/operati...
Author avdv
YourBasic
yourbasic.org › golang › max-min-int-uint
Maximum value of an int · YourBasic Go
This code computes the limit values as untyped constants. const UintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 const ( MaxInt = 1<<(UintSize-1) - 1 // 1<<31 - 1 or 1<<63 - 1 MinInt = -MaxInt - 1 // -1 << 31 or -1 << 63 MaxUint = 1<<UintSize - 1 // 1<<32 - 1 or 1<<64 - 1 ) The UintSize constant is also available in package math/bits. ... Go blueprints: code for common tasks is a collection of handy code examples. Pick the right one: int vs. int64
Educative
educative.io › answers › what-is-type-int64-in-golang
What is type int64 in Golang?
However, just incrementing it by 1 pushed it out of the allowed range, which resulted in the stored value being interpreted as -9223372036854775808 when printed again. When this happens, we call it an overflow.
IncludeHelp
includehelp.com › golang › math-maxuint64-constant-with-examples.aspx
Golang math.MaxUint64 Constant with Examples
The return type of math.MaxUint64 constant is int, it returns the highest (maximum) value that can be represented by a uint64. // Golang program to demonstrate the // example of math.MaxUint64 Constant package main import ( "fmt" "math" ) func main() { fmt.Printf("Type of math.MaxUint64 is ...
Google Groups
groups.google.com › g › golang-nuts › c › a9PitPAHSSU › m › ziQw1-QHw3EJ
[go-nuts] MAX/MIN NUMBER of each integer type
On Apr 16, 12:26 pm, peterGo <go.peter...@gmail.com> wrote: > Daniel > > On Apr 16, 12:12 pm, Ostsol <ost...@gmail.com> wrote: > > > int64, for > > example, has a maximum of 2^32 - 1. > > MaxInt64 = 1<<63 - 1 = 2^64 - 1 > > Peter ... Either email addresses are anonymous for this group or you ...
ZetCode
zetcode.com › golang › builtins-int64-type
Understanding the int64 Type in Golang
package main import "fmt" func main() { var a int64 = 9223372036854775807 // Maximum int64 value var b int64 = -9223372036854775808 // Minimum int64 value c := int64(42) // Type conversion from untyped constant fmt.Println("a:", a) fmt.Println("b:", b) fmt.Println("c:", c) }
Go Packages
pkg.go.dev › builtin
builtin - Go Packages - Golang
If T is a floating-point type and any of the arguments are NaNs, max will return NaN. ... The min built-in function returns the smallest value of a fixed number of arguments of cmp.Ordered types. There must be at least one argument.
Gophersnippets
gophersnippets.com › what-is-the-maximum-value-of-numeric-types
What is the maximum value of numeric types - GopherSnippets.com
package main import ( "fmt" "math" ... fmt.Println("Max float32:", math.MaxFloat32) fmt.Println("Max float64:", math.MaxFloat64) // Output: // Max int8: 127 // Max int16: 32767 // Max int32: 2147483647 // Max int64: 9223372036854775807 // Max uint8: 255 // Max uint16: 65535 ...
Stack Overflow
stackoverflow.com › questions › 77281192 › get-maximum-int64-value-using-2s-complement-arithmetic-golang
go - Get maximum int64 value using 2's complement arithmetic golang - Stack Overflow
... Recommended read: <go.dev/blog/constants>. It deals with a more interesting case and also explains how to work with bit shifts when dealing with constants. ... Found: const MaxInt64 := int64(^uint64(0) >> 1) or const MaxInt64 = 1<<63 - 1, also ...
GitHub
gist.github.com › petergloor › 1eacd3ebcfc3e09ed578dd0d4f80cabe
MaxInt, MinInt, MaxUint and MinUint in Go (golang) · GitHub
April 11, 2022 - package main import "fmt" import "math" const maxUint = uint(math.MaxUint) func main() { fmt.Println("Integer range on your system") // .Println("MaxUint:", math.MaxUint) ERROR constant 18446744073709551615 overflows int fmt.Println("MaxUint:", maxUint) fmt.Println("MinInt:", math.MinInt) fmt.Println("MaxInt:", math.MaxInt) }
GitHub
github.com › go-swagger › go-swagger › issues › 581
max int64 and uint64 handling · Issue #581 · go-swagger/go-swagger
June 29, 2016 - "int64": { 20 "type": "integer", 21 "minimum": -9223372036854775808, 22 "maximum": 9223372036854775807 23 },
Author runcom
Go
go.dev › tour › basics › 11
A Tour of Go
A Tour of Go