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/java/io/protobase/f7/formulas/math/LOG.java
-rw-r--r--
1678
 1package io.protobase.f7.formulas.math;
 2
 3import io.protobase.f7.errors.DivException;
 4import io.protobase.f7.errors.NumException;
 5import io.protobase.f7.formulas.AbstractFormula;
 6import io.protobase.f7.formulas.FormulaName;
 7import io.protobase.f7.models.GridColumnRowKey;
 8import io.protobase.f7.utils.Converters;
 9
10import java.util.function.BiFunction;
11import java.util.function.Function;
12
13public class LOG extends AbstractFormula {
14  public static FormulaName NAME = FormulaName.LOG;
15  public static LOG SELF = new LOG();
16
17  public LOG() {
18    super();
19  }
20
21  public LOG(Function<Object, Object> lookup, BiFunction<GridColumnRowKey, Object, Object> collateralLookup) {
22    super(lookup, collateralLookup);
23  }
24
25  @Override
26  public Object internal(GridColumnRowKey origin, Object... values) {
27    checkLengthBetween(values.length, 1, 2, NAME);
28    Double first = Converters.toDouble(Converters.first(collateralLookup.apply(origin, values[0])));
29    Double second = 10.0;
30    if (values.length > 1) {
31      second = Converters.toDouble(Converters.first(collateralLookup.apply(origin, values[1])));
32    }
33    if (first <= 0) {
34      return new NumException(String.format("LOG parameter 1 value is %f. It should be greater than 0.", first));
35    }
36    if (second <= 0) {
37      return new NumException(String.format("LOG parameter 2 value is %f. It should be greater than 0.", second));
38    }
39    if (second == 10.0) {
40      return Math.log10(first);
41    }
42    Double numerator = Math.log(first);
43    Double denominator = Math.log(second);
44    if (denominator == 0) {
45      throw new DivException("LOG caused a divide by zero error.");
46    }
47    return numerator / denominator;
48  }
49}