The Go programming language uses the Ilogb
function to find the binary exponent of a float64
number. It differs from the Logb
function in its return type, as Ilogb
returns the binary exponent as an integer instead of a float64
.
To use this function, you must import the math
package in your file and access the Ilogb
function within it using the .
notation (math.Ilogb
). Here, Ilogb
is the actual function, while math
is the Go package that stores the definition of this function.
The definition of the Ilogb
function inside the math
package is:
The Ilogb
function takes a single argument of type float64
.
The Ilogb
function returns a single value of type int that represents the binary exponent of the input argument.
The function returns the maximum number that can be stored as an Int32 variable when the input argument has an infinite, undefined, or zero value.
Following is a simple example that generates the binary exponent value of 5:
package mainimport ("fmt""math")func main() {x := 5.0y := math.Ilogb(x)fmt.Print(x, "'s binary exponent value is ", y)}
The following example shows how the Ilogb
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.Ilogb(x)fmt.Print(x, "'s binary exponent value is ", y)fmt.Print( "\n")a := math.Inf(1)b := math.Ilogb(a)fmt.Print(a, "'s binary exponent value is ", b)}
The following examples show how undefined values are handled by the Ilogb
function.
We use the
NaN
function to generate the undefined numeric values to test.
package mainimport ("fmt""math")func main() {x := math.NaN()y := math.Ilogb(x)fmt.Print(x, "'s binary exponent value is ", y)}
Free Resources