×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Exp() Function with Examples

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

math.Exp()

The Exp() function is an inbuilt function of the math package which is used to get the e**x, the base-e exponential of x, where x is the given parameter.

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

Syntax

func Exp(x float64) float64

Parameters

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

Return Value

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

Note: Very large values overflow to 0 or +Inf. Very small values underflow to 1.

Special cases are:

  • Exp(+Inf) = +Inf
    If the parameter is the positive infinity, the function returns the same (+Inf).
  • Exp(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.Exp() Function

package main

import (
	"fmt"
	"math"
)

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

	fmt.Println(math.Exp(math.Inf(1)))
	fmt.Println(math.Exp(math.NaN()))
}

Output:

1
148.4131591025766
0.006737946999085467
36315.502674246636
2.7536449349747158e-05
+Inf
NaN

Example 2

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var ExpX float64

	x = 0
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)

	x = 0.5
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)

	x = 10.5
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)

	x = -10.5
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)

	x = -0.15
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)

	x = math.NaN()
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)

	x = math.Inf(1)
	ExpX = math.Exp(x)
	fmt.Println("Exp(", x, ") = ", ExpX)
}

Output:

Exp( 0 ) =  1
Exp( 0.5 ) =  1.6487212707001282
Exp( 10.5 ) =  36315.502674246636
Exp( -10.5 ) =  2.7536449349747158e-05
Exp( -0.15 ) =  0.8607079764250578
Exp( NaN ) =  NaN
Exp( +Inf ) =  +Inf

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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