Golang math.Logb() Function with Examples

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

math.Logb()

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

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

Syntax:

func Logb(x float64) float64

Parameter(s):

  • x : The value whose binary exponent is to be found.

Return Value:

The return type of Logb() function is a float64, it returns the binary exponent of the given value.

Special cases:

  • Logb(±Inf) = +Inf
    If the parameter is either positive infinity or negative infinity (±Inf), the function return the positive infinity (+Inf).
  • Logb(0) = -Inf
    If the parameter is 0, the function returns the negative infinity (-Inf).
  • Logb(NaN) = NaN
    If the parameter is NaN, the function returns the same (NaN).

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Logb(1))
	fmt.Println(math.Logb(2))
	fmt.Println(math.Logb(10))
	fmt.Println(math.Logb(10.23))

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

Output:

0
1
3
3
-Inf
0
NaN
+Inf

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var LogbX float64

	x = 1
	LogbX = math.Logb(x)
	fmt.Println("Logb(", x, ") =", LogbX)

	x = 2
	LogbX = math.Logb(x)
	fmt.Println("Logb(", x, ") =", LogbX)

	x = 10
	LogbX = math.Logb(x)
	fmt.Println("Logb(", x, ") =", LogbX)

	x = 10.58
	LogbX = math.Logb(x)
	fmt.Println("Logb(", x, ") =", LogbX)

	x = math.Inf(1)
	LogbX = math.Logb(x)
	fmt.Println("Logb(", x, ") =", LogbX)

	x = math.NaN()
	LogbX = math.Logb(x)
	fmt.Println("Logb(", x, ") =", LogbX)
}

Output:

Logb( 1 ) = 0
Logb( 2 ) = 1
Logb( 10 ) = 3
Logb( 10.58 ) = 3
Logb( +Inf ) = +Inf
Logb( NaN ) = NaN

Golang math Package Constants and Functions »





Comments and Discussions!

Load comments ↻






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