Skip to content

Instantly share code, notes, and snippets.

@Adron
Created March 1, 2019 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adron/a43e7090703d8361cd3a5451602ecbde to your computer and use it in GitHub Desktop.
Save Adron/a43e7090703d8361cd3a5451602ecbde to your computer and use it in GitHub Desktop.
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