-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add NaturalLog node class (#13)
* feat: add ln function node class * fix: add .DS_Store ignore in .gitignore * fix: fix typo in some files * test: Add more test assertion to NaturalLog * fix: remove unecessary Decimal convertion
- Loading branch information
1 parent
bb1ee54
commit 9ef3600
Showing
5 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Decimal from 'decimal.js'; | ||
import TreeNode, { ITreeNode } from '../TreeNode'; | ||
|
||
/** | ||
* NaturalLog node | ||
* @class NaturalLog | ||
*/ | ||
|
||
export default class NaturalLog extends TreeNode implements ITreeNode { | ||
operand : TreeNode; | ||
|
||
constructor(...x : TreeNode[]) { | ||
if (x.length !== 1) { | ||
throw new SyntaxError(`ln takes exactly one argument, received ${x.length}`); | ||
} | ||
super('ln'); | ||
[this.operand] = x; | ||
} | ||
|
||
/** | ||
* Evaluates the natural log function | ||
* @returns the result of the natural log function | ||
*/ | ||
evaluate() { | ||
return Decimal.ln(this.operand.evaluate()).toString(); | ||
} | ||
|
||
/** | ||
* print the abstract syntax tree of the natural log function | ||
* @returns the string representation of the natural log function | ||
*/ | ||
toString() { | ||
return `ln(${this.operand.toString()})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
describe, expect, it, | ||
} from 'vitest'; | ||
import TreeNode from '../../../src/nodes/TreeNode'; | ||
import { Expression } from '../../../src/Expression'; | ||
import NaturalLog from '../../../src/nodes/Functions/NaturalLog'; | ||
|
||
const a = new TreeNode('2'); | ||
|
||
describe('NaturalLog', () => { | ||
it('can print out the abstract syntax tree', () => { | ||
expect(new NaturalLog(a).toString()).toEqual('ln(2)'); | ||
}); | ||
it('can evaluate the abstract syntax tree', () => { | ||
Expression.config({ precision: 3 }); | ||
expect(new NaturalLog(a).evaluate()).toEqual('0.693'); | ||
expect(new Expression('ln(1)').evaluate()).toEqual('0'); | ||
expect(new Expression('ln(2π)').evaluate()).toEqual('1.84'); | ||
expect(new Expression('ln(ln(1))').evaluate()).toEqual('-Infinity'); | ||
}); | ||
}); |