Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit73bf91d

Browse files
Add an algorithm to find Taylor series approximation of exponential f… (TheAlgorithms#1160)
1 parent5867186 commit73bf91d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

‎Maths/ExponentialFunction.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*@function exponentialFunction
3+
*@description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
4+
*@param {Integer} power
5+
*@param {Integer} order - 1
6+
*@returns exponentialFunction(2,20) = 7.3890560989301735
7+
*@url https://en.wikipedia.org/wiki/Exponential_function
8+
*/
9+
functionexponentialFunction(power,n){
10+
letoutput=0
11+
letfac=1
12+
if(isNaN(power)||isNaN(n)||n<0){
13+
thrownewTypeError('Invalid Input')
14+
}
15+
if(n===0){return1}
16+
for(leti=0;i<n;i++){
17+
output+=(power**i)/fac
18+
fac*=(i+1)
19+
}
20+
returnoutput
21+
}
22+
23+
export{
24+
exponentialFunction
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import{exponentialFunction}from'../ExponentialFunction'
2+
3+
describe('Tests for exponential function',()=>{
4+
it('should be a function',()=>{
5+
expect(typeofexponentialFunction).toEqual('function')
6+
})
7+
8+
it('should throw error for invalid input',()=>{
9+
expect(()=>exponentialFunction(2,-34)).toThrow()
10+
})
11+
12+
it('should return the exponential function of power of 5 and order of 21',()=>{
13+
constex=exponentialFunction(5,20)
14+
expect(ex).toBe(148.4131078683383)
15+
})
16+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp