ADVERTISEMENT
ADVERTISEMENT

Golang math.Log1p() Function with Examples

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

math.Log1p()

The Log1p() function is an inbuilt function of the math package which is used to get the natural logarithm of 1 plus given value. It is more accurate than Log(1 + x) when x is near zero.

It accepts a parameter (x) and returns the logarithm of 1 plus its argument x.

Syntax:

func Log1p(x float64) float64

Parameter(s):

  • x : The value whose logarithm of (1+x) is to be found.

Return Value:

The return type of Log1p() function is a float64, it returns the logarithm of 1 plus given value.

Special cases:

  • Log1p(+Inf) = +Inf
    If the parameter +Inf, the function returns the same (+Inf).
  • Log1p(±0) = ±0
    If the parameter is ±0, the function returns the same (±0).
  • Log1p(-1) = -Inf
    If the parameter is -1, the function returns -Inf.
  • Log1p(x < -1) = NaN
    If the parameter is less than -1, the function returns NaN.
  • Log1p(NaN) = NaN
    If the parameter is NaN, the function returns NaN.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

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

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

Output:

0.6931471805599453
1.0986122886681096
2.3978952727983707
2.418588768750352
0
-Inf
NaN
+Inf

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var Log1pX float64

	x = 1
	Log1pX = math.Log1p(x)
	fmt.Println("Log1p(", x, ") =", Log1pX)

	x = 2
	Log1pX = math.Log1p(x)
	fmt.Println("Log1p(", x, ") =", Log1pX)

	x = 10
	Log1pX = math.Log1p(x)
	fmt.Println("Log1p(", x, ") =", Log1pX)

	x = 10.58
	Log1pX = math.Log1p(x)
	fmt.Println("Log1p(", x, ") =", Log1pX)

	x = math.Inf(1)
	Log1pX = math.Log1p(x)
	fmt.Println("Log1p(", x, ") =", Log1pX)

	x = math.NaN()
	Log1pX = math.Log1p(x)
	fmt.Println("Log1p(", x, ") =", Log1pX)
}

Output:

Log1p( 1 ) = 0.6931471805599453
Log1p( 2 ) = 1.0986122886681096
Log1p( 10 ) = 2.3978952727983707
Log1p( 10.58 ) = 2.449279472144849
Log1p( +Inf ) = +Inf
Log1p( 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.