Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

# What is the Logb function in golang?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Go programming language uses the Logb function to find the binary exponent of a float64 number.

To use this function, one must import the math package in your file and access the Logb function within it using the . notation (math.Logb). Here, Logb is the actual function, while math is the Go package that stores the definition of this function.

## Function definition

The definition of the Logb function inside the math package is as follows:

## Parameters

Logb function takes a single argument of type float64.

## Return value

The Logb function returns a single value of type float64, representing the input argument’s binary exponent.

The few cases when the Logb function returns special values are when the values passed to it are undefined, infinite, or zero:

• +Inf: If the passed argument has infinite value (positive or negative), positive infinity is returned.

• 0: If the passed argument has zero value, negative infinity is returned.

• NAN: If the passed argument has an undefined value (a NAN value), a NAN is returned as well.

## Examples

Following is a simple example that generates the binary exponent value of 5:

package mainimport (	"fmt"	"math")func main() {	x := 5.0	y := math.Logb(x)	fmt.Print(x, "'s binary exponent value is ", y)}

The following example shows how the Logb function handles infinite values:

The Inf function returns an infinite value with a sign matching the sign of the argument that it is given.

package mainimport (	"fmt"	"math")func main() {	x := math.Inf(-1)	y := math.Logb(x)	fmt.Print(x, "'s binary exponent value is ", y)	fmt.Print( "\n")	a := math.Inf(1)	b := math.Logb(a)	fmt.Print(a, "'s binary exponent value is ", b)}

The following examples show how the Logb function handles undefined values:

We use the NaN function to generate the undefined numeric values to test.

package mainimport (	"fmt"	"math")func main() {	x := math.NaN()	y := math.Logb(x)	fmt.Print(x, "'s binary exponent value is ", y)}

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim 