ADVERTISEMENT
ADVERTISEMENT

Golang math.Log2() Function with Examples

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

math.Log2()

The Log2() function is an inbuilt function of the math package which is used to get the binary logarithm (base-2 logarithm) of the given number.

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

Syntax:

func Log2(x float64) float64

Parameter(s):

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

Return Value:

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

Special cases:

  • Log2(+Inf) = +Inf
    If the parameter is positive infinity (+Inf), the function returns the same (+Inf).
  • Log2(0) = -Inf
    If the parameter is 0, the function returns the -Inf.
  • Log2(x < 0) = NaN
    If the parameter is less than 0, the function returns NaN.
  • Log2(NaN) = NaN
    If the parameter is NaN, the function returns NaN.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

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

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

Output:

0
1
3.321928094887362
3.354734239970604
-Inf
NaN
NaN
+Inf

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var Log2X float64

	x = 1
	Log2X = math.Log2(x)
	fmt.Println("Log2(", x, ") =", Log2X)

	x = 2
	Log2X = math.Log2(x)
	fmt.Println("Log2(", x, ") =", Log2X)

	x = 10
	Log2X = math.Log2(x)
	fmt.Println("Log2(", x, ") =", Log2X)

	x = 10.58
	Log2X = math.Log2(x)
	fmt.Println("Log2(", x, ") =", Log2X)

	x = math.Inf(1)
	Log2X = math.Log2(x)
	fmt.Println("Log2(", x, ") =", Log2X)

	x = math.NaN()
	Log2X = math.Log2(x)
	fmt.Println("Log2(", x, ") =", Log2X)
}

Output:

Log2( 1 ) = 0
Log2( 2 ) = 1
Log2( 10 ) = 3.321928094887362
Log2( 10.58 ) = 3.403267722339301
Log2( +Inf ) = +Inf
Log2( 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.