For example,

package main

import (
    "fmt"
    "math"
)

func main() {
    const f = math.MaxFloat64
    fmt.Printf("%[1]T %[1]v\n", f)
    const c = complex(math.MaxFloat64, math.MaxFloat64)
    fmt.Printf("%[1]T %[1]v\n", c)
}

Output:

float64 1.7976931348623157e+308
complex128 (1.7976931348623157e+308+1.7976931348623157e+308i)

Package math

import "math" 

Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

const (
        MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
        SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

        MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
        SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)

The Go Programming Language Specification

Numeric types

A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

There is also a set of predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int      same size as uint
uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value

To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

Answer from peterSO on Stack Overflow
Top answer
1 of 2
40

For example,

package main

import (
    "fmt"
    "math"
)

func main() {
    const f = math.MaxFloat64
    fmt.Printf("%[1]T %[1]v\n", f)
    const c = complex(math.MaxFloat64, math.MaxFloat64)
    fmt.Printf("%[1]T %[1]v\n", c)
}

Output:

float64 1.7976931348623157e+308
complex128 (1.7976931348623157e+308+1.7976931348623157e+308i)

Package math

import "math" 

Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

const (
        MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
        SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

        MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
        SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)

The Go Programming Language Specification

Numeric types

A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

There is also a set of predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int      same size as uint
uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value

To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

2 of 2
5

You can also consider using the Inf method from the math package which returns a value for infinity (positive or negative if you want), but is considered to be float64.

Not too sure if there is an argument for one or the other between math.MaxFloat64 and math.Inf(). Comparing the two I've found that Go interprets the infinity values to be larger than the max float ones.

package main

import (
    "fmt"
    "math"
)

func main() {
    infPos := math.Inf(1) // gives positive infinity
    fmt.Printf("%[1]T %[1]v\n", infPos)
    
    infNeg := math.Inf(-1) // gives negative infinity
    fmt.Printf("%[1]T %[1]v\n", infNeg)
}

🌐
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 -

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

My 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
}
Discussions

numbers - The maximum value for an int type in Go - Stack Overflow
How does one specify the maximum value representable for an unsigned integer type? I would like to know how to initialize min in the loop below that iteratively computes min and max lengths from s... More on stackoverflow.com
🌐 stackoverflow.com
math: MaxFloat32/64 and SmallestNonzeroFloat32/64 value comments misleading
What version of Go are you using (go version)? Playground 1.20 as of Fri Jul 7 15:17:35 UTC 2023. Does this issue reproduce with the latest release? Yes. What operating system and processor archite... More on github.com
🌐 github.com
3
July 7, 2023
cmd/gc: conversion of const beyond max float32 gives zero
Search the issue tracker and check ... http://golang.org/doc/devel/release.html If a newer version of Go exists, install it and retry what you did to reproduce the problem. Thanks. What does 'go version' print? TIP on darwin/amd64 "go version devel +386c6757d94c Sat May 17 00:06:56 2014 +0000 darwin/amd64" The available maximum float32 should be ... More on github.com
🌐 github.com
11
May 17, 2014
Don't abuse math max, min functions
Good, thorough explanation. Thanks More on reddit.com
🌐 r/golang
54
19
July 18, 2015
🌐
Educative
educative.io › answers › what-is-type-float32-in-golang
What is type float32 in Golang?
The following code shows what happens ... the outputs of the code above, when we stored the max magnitude of a negative number of -3.4E+38 in num and printed it, the value came ......
🌐
gosamples
gosamples.dev › tutorials › the maximum and minimum value of the float types in go
📊 The maximum and minimum value of the float types in Go
April 14, 2022 - The maximum value of the float32 type in Go is 3.40282346638528859811704183484516925440e+38 and you can get this value using the math.MaxFloat32 constant.
🌐
W3Schools
w3schools.com › go › go_float_data_type.php
Go Float Data Types
The type of float to choose, depends on the value the variable has to store. This example will result in an error because 3.4e+39 is out of range for float32:
🌐
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
🌐
LabEx
labex.io › tutorials › go-numerical-types-in-golang-149067
Numerical Types in Golang | LabEx
The maximum value of float32: 340282346638528859811704183484516925440.000000: This shows the maximum value that a float32 can represent.
Find elsewhere
🌐
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 ...
🌐
GitHub
github.com › golang › go › issues › 61230
math: MaxFloat32/64 and SmallestNonzeroFloat32/64 value comments misleading · Issue #61230 · golang/go
July 7, 2023 - golang / go Public · Notifications · You must be signed in to change notification settings · Fork 18.9k · Star 133k · New issueCopy link · New issueCopy link · Closed · Closed · math: MaxFloat32/64 and SmallestNonzeroFloat32/64 value ...
Author   extemporalgenome
🌐
Go Packages
pkg.go.dev › git.maze.io › go › math32
math32 package - git.maze.io/go/math32 - Go Packages
Max(x, +Inf) = Max(+Inf, x) = +Inf Max(x, NaN) = Max(NaN, x) = NaN Max(+0, ±0) = Max(±0, +0) = +0 Max(-0, -0) = -0 ... Min returns the smaller of x or y. ... Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x. ... Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f. ... NaN returns an IEEE 754 “not-a-number” value. ... Nextafter returns the next representable float32 value after x towards y.
🌐
Go Packages
pkg.go.dev › math
math package - math - Go Packages
Note that this differs from the built-in function max when called with NaN and +Inf. ... Min returns the smaller of x or y. ... Note that this differs from the built-in function min when called with NaN and -Inf. ... Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x. ... Modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.
🌐
Go Packages
pkg.go.dev › math › big
big package - math/big - Go Packages
Float32 returns the float32 value nearest to x. If x is too small to be represented by a float32 (|x| < math.SmallestNonzeroFloat32), the result is (0, Below) or (-0, Above), respectively, depending on the sign of x. If x is too large to be represented by a float32 (|x| > math.MaxFloat32), the result is (+Inf, Above) or (-Inf, Below), depending on the sign of x.
🌐
Leapcell
leapcell.io › blog › understanding-floating-point-numbers-in-go
Understanding Floating-Point Numbers in Go | Leapcell
July 25, 2025 - On the other hand, float64, or double-precision, occupies 8 bytes and provides about 15 decimal digits of precision. The range of values these types can represent is vast. For instance, the maximum value for float32 is approximately 3.4e38, while ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-data-types-in-go
Understanding Data Types in Go | DigitalOcean
May 1, 2019 - Go has the potential to both overflow a number and wraparound a number when you try to store a value larger than the data type was designed to store, depending on if the value is calculated at compile time or at runtime. A compile time error happens when the program finds an error as it tries to build the program. A runtime error happens after the program is compiled, while it is actually executing. In the following example, we set maxUint32 to its maximum value:
🌐
Blogger
mrekucci.blogspot.com › 2015 › 07 › dont-abuse-mathmax-mathmin.html
mrekucci's blog: Don't abuse math.Max / math.Min
Many people, especially newcomers to Go (of course, not all of them), tend to abuse the math.Min and math.Max functions from the math p...
🌐
Go Packages
pkg.go.dev › github.com › emer › etable › minmax
minmax package - github.com/emer/etable/minmax - Go Packages
This is used for choosing graph labels, and auto-scaling ranges to contain a given value. if below == true then returned number is strictly less than given number otherwise it is strictly larger. type AvgMax32 struct { Avg float32 Max float32 // sum for computing average Sum float32 `desc:"sum for computing average"` // index of max item MaxIdx int32 `desc:"index of max item"` // number of items in sum N int32 `desc:"number of items in sum"` // contains filtered or unexported fields }
🌐
GitHub
github.com › golang › go › issues › 8015
cmd/gc: conversion of const beyond max float32 gives zero · Issue #8015 · golang/go
May 17, 2014 - TIP on darwin/amd64 "go version devel +386c6757d94c Sat May 17 00:06:56 2014 +0000 darwin/amd64" The available maximum float32 should be 340282356779733661637539395458142568447 = (1 - 2^(-25))*2^(2^7) - 1 Actually, any number bigger than ...
Published   May 17, 2014
Author   gopherbot
🌐
Go Packages
pkg.go.dev › go.skia.org › infra › go › vec32
vec32 package - go.skia.org/infra/go/vec32 - Go Packages
Max returns the largest value in the vector, or math.MinFloat32 if no non-MissingDataSentinel values are found. ... Mean calculates and returns the Mean value of the given []float32.