Home » How to Get the Maximum and Minimum Value of Float Types in Golang

How to Get the Maximum and Minimum Value of Float Types in Golang

To get the max value of float types and min value of float types in golang:

  • Use and import the math package
  • math package has constants for float limit values.
  • Use math.MaxFloat64 to get the max float64 value which is 1.79769313486231570814527423731704356798070e+308 
  • math constants for max and min values are available for different integer types like MaxFloat32, SmallestNonzeroFloat32, MaxFloat64, SmallestNonzeroFloat64

Refer to the following table to get maximum and minimum values for the float types in golang.

ConstantValueFloat Value
MaxFloat320x1p127 * (1 + (1 – 0x1p-23))3.40282346638528859811704183484516925440e+38
SmallestNonzeroFloat320x1p-126 * 0x1p-231.401298464324817070923729583289916131280e-45
MaxFloat640x1p1023 * (1 + (1 – 0x1p-52))1.79769313486231570814527423731704356798070e+308
SmallestNonzeroFloat640x1p-1022 * 0x1p-524.9406564584124654417656879286822137236505980e-324
Golang float64 Max value and float64 Min int value

In this article, we will discuss max float64 types values in golang, min float64 values, and how to get math constants min, and maximum float values in the golang program.

How to Get Max Float and Min Float Type Value in Golang

To get the max and min values of float64 types in golang, refer to the following golang program.

package main

import (
	"fmt"
	"math"
)

func main() {

	// MaxFloat32
	fmt.Printf("MaxFloat32 max value: %.50e\n", math.MaxFloat32)

	// MinFloat32
	fmt.Printf("MinFloat32 min value: %.50e\n", math.SmallestNonzeroFloat32)

	// MaxFloat64
	fmt.Printf("MaxFloat64 max value: %.50e\n", math.MaxFloat64)

	// MinFloat64
	fmt.Printf("MinFloat64 min value: %.50e\n", math.SmallestNonzeroFloat64)

}

Output

MaxFloat32 max value: 3.40282346638528859811704183484516925440000000000000e+38
MinFloat32 min value: 1.40129846432481707092372958328991613128026194187652e-45
MaxFloat64 max value: 1.79769313486231570814527423731704356798070567525845e+308
MinFloat64 min value: 4.94065645841246544176568792868221372365059802614325e-324

In the above golang program, we import the math package and use the mathematical constant to get maximum and minimum float type values in go.

Conclusion

I hope the above article helped you to understand max float type values in golang and minimum float type values using the golang program.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Recommended Content

Golang Max Value of Int and Min Value of Int type

Leave a Comment