Skip to content

Instantly share code, notes, and snippets.

@Adron

Adron/main.go

Created Mar 1, 2019
Embed
What would you like to do?
Getting min max for various types in Go
package main
import (
"fmt"
"math"
)
const(
MinUint uint = 0 // all zeroes
// Perform a bitwise NOT to change every bit from 0 to 1.
MaxUint = ^MinUint // all ones
// Shift the binary number to the right to get the high bit to zero
MaxInt = int(MaxUint >> 1) // all ones except high bit
MinInt = ^MaxInt // all zeroes except high bit.
)
func main() {
// integer max
fmt.Printf("max for int64 = %+v\n", math.MaxInt64)
fmt.Printf("max for int32 = %+v\n", math.MaxInt32)
fmt.Printf("max for int16 = %+v\n", math.MaxInt16)
fmt.Printf("max for int8 = %+v\n", math.MaxInt8)
// integer min
fmt.Printf("min for int64 = %+v\n", math.MinInt64)
fmt.Printf("min for int32 = %+v\n", math.MinInt32)
fmt.Printf("min for int16 = %+v\n", math.MinInt16)
fmt.Printf("min for int8 = %+v\n", math.MinInt8)
// Added parts that aren't in the video!
fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
fmt.Printf("max float32 = %+v\n", math.MaxFloat32)
fmt.Printf("smallest nonzero float64 = %+v\n", math.SmallestNonzeroFloat64)
fmt.Printf("smallest nonzero float32 = %+v\n", math.SmallestNonzeroFloat32)
fmt.Printf("max uint = %+v\n", MaxUint)
fmt.Printf("min uint = %+v\n", MinUint)
fmt.Printf("max int = %+v\n", MaxInt)
fmt.Printf("min int = %+v\n", MinInt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment