Skip to content

Commit

Permalink
feat: add NaturalLog node class (#13)
Browse files Browse the repository at this point in the history
* 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
fazzaamiarso committed Oct 1, 2022
1 parent bb1ee54 commit 9ef3600
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,8 @@ new Expression('sin(30)').evaluate(); // 0.5
<td>tan</td>
<td>returns the tangent of an expression</td>
</tr>
<tr>
<td>ln</td>
<td>returns the natural log of an expression</td>
</tr>
</table>
1 change: 1 addition & 0 deletions src/constants/nodeMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const FUNC_NODE_MAP:{[key:string]:(x:ITreeNode[])=>ITreeNode} = {
asin: (x:ITreeNode[]) => new Nodes.Asin(...x),
acos: (x:ITreeNode[]) => new Nodes.Acos(...x),
atan: (x:ITreeNode[]) => new Nodes.Atan(...x),
ln: (x:ITreeNode[]) => new Nodes.NaturalLog(...x),
};

export const UNARY_OPERATOR_PREC:IOperator = {
Expand Down
35 changes: 35 additions & 0 deletions src/nodes/Functions/NaturalLog.ts
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()})`;
}
}
3 changes: 2 additions & 1 deletion src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import Tan from './Functions/Tan';
import Asin from './Functions/Asin';
import Acos from './Functions/Acos';
import Atan from './Functions/Atan';
import NaturalLog from './Functions/NaturalLog';
import Constant from './Constant';

export {
Add, Divide, Multiply, Subtract, Modulo, TreeNode, Power, Sine, Cosine, Tan, Factorial,
Gammln, Constant, Max, Min, Asin, Acos, Atan,
Gammln, Constant, Max, Min, Asin, Acos, Atan, NaturalLog,
};
21 changes: 21 additions & 0 deletions test/nodes/Functions/NaturalLog.test.ts
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');
});
});

0 comments on commit 9ef3600

Please sign in to comment.