×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Ilogb() Function with Examples

Golang | math.Ilogb() Function: Here, we are going to learn about the Ilogb() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 01, 2021

math.Ilogb()

The Ilogb() function is an inbuilt function of the math package which is used to get the binary exponent of the given number as an integer.

It accepts a parameter (x) and returns the binary exponent of x as an integer.

Syntax

func Ilogb(x float64) int

Parameters

  • x : The values whose binary exponent an integer is to be found.

Return Value

The return type of Ilogb() function is an int, it returns the binary exponent of the given number as an integer.

Special Cases

  • Ilogb(±Inf) = MaxInt32
    If the parameter is ±Inf, the function returns the highest value of the Int32 (MaxInt32).
  • Ilogb(0) = MinInt32
    If the parameter is 0, the function returns the lowest value of the Int32 (MinInt32).
  • Ilogb(NaN) = MaxInt32
    If the parameter is NaN, the function returns the highest value of the Int32 (MaxInt32).

Example 1

// Golang program to demonstrate the
// example of math.Ilogb() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Ilogb(16.40))
	fmt.Println(math.Ilogb(64.50))
	fmt.Println(math.Ilogb(-128.01))

	fmt.Println(math.Ilogb(0))
	fmt.Println(math.Ilogb(-1))
	fmt.Println(math.Ilogb(math.Inf(-1)))
	fmt.Println(math.Ilogb(math.Inf(+1)))
	fmt.Println(math.Ilogb(math.NaN()))
}

Output:

4
6
7
-2147483648
0
2147483647
2147483647
2147483647

Example 2

// Golang program to demonstrate the
// example of math.Ilogb() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var IlogbX int

	x = 16
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = 32.50
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = 64.60
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = -128.10
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = 0
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = -1
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = math.Inf(1)
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)

	x = math.NaN()
	IlogbX = math.Ilogb(x)
	fmt.Println("Ilogb(", x, ") = ", IlogbX)
}

Output:

Ilogb( 16 ) =  4
Ilogb( 32.5 ) =  5
Ilogb( 64.6 ) =  6
Ilogb( -128.1 ) =  7
Ilogb( 0 ) =  -2147483648
Ilogb( -1 ) =  0
Ilogb( +Inf ) =  2147483647
Ilogb( NaN ) =  2147483647

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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