ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.