f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/js/formulas/math/LOG.ts
-rw-r--r--
1438
 1import { DivException } from "../../errors/DivException";
 2import { NumException } from "../../errors/NumException";
 3import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
 4import { Complex } from "../../models/common/Types";
 5import { Converters } from "../../utils/Converters";
 6import { AbstractFormula } from "../AbstractFormula";
 7import { FormulaName } from "../FormulaName";
 8
 9export class LOG extends AbstractFormula {
10  static SELF: LOG = new LOG();
11  NAME = FormulaName.LOG;
12
13  internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
14    AbstractFormula.checkLengthBetween(values.length, 1, 2, this.NAME);
15    const first = Converters.toNumber(Converters.first(this.collateralLookup(origin, values[0])));
16    let second = 10.0;
17    if (values.length > 1) {
18      second = Converters.toNumber(Converters.first(this.collateralLookup(origin, values[1])));
19    }
20    if (first <= 0) {
21      return new NumException(`LOG parameter 1 value is ${first}. It should be greater than 0.`);
22    }
23    if (second <= 0) {
24      return new NumException(`LOG parameter 2 value is ${second}. It should be greater than 0.`);
25    }
26    if (second == 10.0) {
27      return Math["log10"](first);
28    }
29    const numerator = Math.log(first);
30    const denominator = Math.log(second);
31    if (denominator == 0) {
32      throw new DivException("LOG caused a divide by zero error.");
33    }
34    return numerator / denominator;
35  }
36}