×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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

Parameters

  • 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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.