×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Expm1() Function with Examples

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

math.Expm1()

The Expm1() function is an inbuilt function of the math package which is used to get the e**x - 1, the base-e exponential of x minus 1. Where x is the given parameter. It is more accurate than Exp(x) - 1 when x is near zero.

It accepts a parameter (x) and returns the e**x - 1, the base-e exponential of x minus 1.

Syntax

func Expm1(x float64) float64

Parameters

  • x : The value of float64 type whose base-e exponential of x minus 1 is to be found.

Return Value

The return type of Expm1() function is a float64, it returns the e**x - 1, the base-e exponential of x minus 1.

Note: Very large values overflow to -1 or +Inf.

Special cases are:

  • Expm1(+Inf) = +Inf
    If the parameter is the positive infinity, the function returns the same (+Inf).
  • Expm1(-Inf) = -1
    If the parameter is the negative infinity, the function returns -1.
  • Expm1(NaN) = NaN
    If the parameter is NaN (Not-A-Number), the function returns the same (NaN).

Example 1

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Expm1(0))
	fmt.Println(math.Expm1(5))
	fmt.Println(math.Expm1(-5))
	fmt.Println(math.Expm1(10.5))
	fmt.Println(math.Expm1(-10.5))
	fmt.Println(math.Expm1(0.01))

	fmt.Println(math.Expm1(math.Inf(1)))
	fmt.Println(math.Expm1(math.Inf(-1)))
	fmt.Println(math.Expm1(math.NaN()))
}

Output:

0
147.4131591025766
-0.9932620530009145
36314.502674246636
-0.9999724635506503
0.010050167084168058
+Inf
-1
NaN

Example 2

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var Expm1X float64

	x = 0
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = 0.5
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = 6
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = -6
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = -0.15
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = math.NaN()
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = math.Inf(1)
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)

	x = math.Inf(-1)
	Expm1X = math.Expm1(x)
	fmt.Println("Expm1(", x, ") = ", Expm1X)
}

Output:

0
147.4131591025766
-0.9932620530009145
36314.502674246636
-0.9999724635506503
0.010050167084168058
+Inf
-1
NaN

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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