ADVERTISEMENT
ADVERTISEMENT

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

Parameter(s):

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


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.