ADVERTISEMENT
ADVERTISEMENT

Golang math.Log() Function with Examples

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

math.Log()

The Log() function is an inbuilt function of the math package which is used to get the natural logarithm of the given number.

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

Syntax:

func Log(x float64) float64

Parameter(s):

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

Return Value:

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

Special cases:

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

Example 1:

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

package main

import (
	"fmt"
	"math"
)

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

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

Output:

0
0.6931471805599453
2.302585092994046
2.325324579963535
-Inf
NaN
NaN
+Inf

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var LogX float64

	x = 1
	LogX = math.Log(x)
	fmt.Println("Log(", x, ") =", LogX)

	x = 2
	LogX = math.Log(x)
	fmt.Println("Log(", x, ") =", LogX)

	x = 10
	LogX = math.Log(x)
	fmt.Println("Log(", x, ") =", LogX)

	x = 10.58
	LogX = math.Log(x)
	fmt.Println("Log(", x, ") =", LogX)

	x = math.Inf(1)
	LogX = math.Log(x)
	fmt.Println("Log(", x, ") =", LogX)

	x = math.NaN()
	LogX = math.Log(x)
	fmt.Println("Log(", x, ") =", LogX)
}

Output:

Log( 1 ) = 0
Log( 2 ) = 0.6931471805599453
Log( 10 ) = 2.302585092994046
Log( 10.58 ) = 2.3589654264301534
Log( +Inf ) = +Inf
Log( 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.