Golang math.MaxInt Constant with Examples

Golang | math.MaxInt Constant: Here, we are going to learn about the MaxInt constant of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 26, 2021

math.MaxInt Constant

The MaxInt constant is an inbuilt constant of the math package which is used to get the highest (maximum) value that can be represented by an int.

The value of math.MaxInt constants is 1<<(intSize-1) - 1.

Note: intSize depends on the machine architecture. For example, if machine is a 32-bit machine, intSize will be 4 bytes and if the machine is 64-bit, intSize will be 8 bytes.

Syntax:

int math.MaxInt

Parameter(s):

  • None

Return Value:

The return type of math.MaxInt constant is int, it returns the highest (maximum) value that can be represented by an int.

Example 1:

// Golang program to demonstrate the
// example of math.MaxInt Constant

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("Type of math.MaxInt is %T\n", math.MaxInt)
	fmt.Println("Value of math.MaxInt:", math.MaxInt)
}

Output:

Type of math.MaxInt is int
Value of math.MaxInt: 9223372036854775807

Explanation:

In the above program, we imported the math package to use the math.MaxInt constant, then printed the type and value of the math.MaxInt constant.

Example 2:

// Golang program to demonstrate the
// example of math.MaxInt Constant

package main

import (
	"fmt"
	"math"
)

// creating function to
// return the value of MaxInt.
func getMaxInt() int {
	return math.MaxInt
}

func main() {
	fmt.Println("Value of math.MaxInt:", getMaxInt())
}

Output:

Value of math.MaxInt: 9223372036854775807

Golang math Package Constants and Functions »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.