29.仿简道云公式函数实战-数学函数-LOG

1. LOG函数

根据指定底数返回数字的对数。

2.函数用法

LOG(number, [base])

3. 函数示例

根据指定底数返回数字的对数。

number: 必需。 想要计算其对数的正实数。

base: 可选。 对数的底数。 如果省略 base,则假定其值为 10。

4. 代码实战

首先我们在function包下创建math包,在math包下创建LogFunction类,代码如下:

package com.ql.util.express.self.combat.function.math;

import com.ql.util.express.Operator;
import com.ql.util.express.self.combat.exception.FormulaException;

import java.math.BigDecimal;
import java.math.MathContext;

/**
 * 类描述: LOG函数
 *
 * @author admin
 * @version 1.0.0
 * @date 2023/11/23 13:45
 */
public class LogFunction extends Operator {

    public LogFunction(String name) {
        this.name = name;
    }

    @Override
    public Object executeInner(Object[] list) throws Exception {

        if (list.length <= 0 || list.length >2) {
            throw new FormulaException("操作数异常");
        }

        BigDecimal number = new BigDecimal(list[0].toString());
        BigDecimal rst = BigDecimal.ZERO;
        if (list.length == 1) {
            BigDecimal base = BigDecimal.TEN;
            rst = log(number,base);
        } else {
            BigDecimal base = new BigDecimal(list[1].toString());
            rst = log(number,base);
        }
        return rst;
    }

    public static BigDecimal log(BigDecimal number, BigDecimal base) {
        BigDecimal result = BigDecimal.ZERO;
        BigDecimal logBase = BigDecimal.valueOf(Math.log(base.doubleValue()));

        MathContext mathContext = new MathContext(100); // 设置精度为100
        BigDecimal epsilon = BigDecimal.valueOf(1e-50); // 设置一个较小的误差范围

        while (number.compareTo(base) >= 0) {
            result = result.add(BigDecimal.ONE, mathContext);
            number = number.divide(base, mathContext);
        }

        BigDecimal fraction = BigDecimal.ZERO;
        BigDecimal power = BigDecimal.ONE;

        while (power.compareTo(epsilon) > 0) {
            power = power.divide(base, mathContext);

            if (number.compareTo(BigDecimal.ONE) >= 0) {
                number = number.divide(base, mathContext);
                fraction = fraction.add(power, mathContext);
            }
        }
        return result;
    }

}

把LogFunction类注册到公式函数入口类中,代码如下:

package com.ql.util.express.self.combat.ext;

import com.ql.util.express.ExpressRunner;
import com.ql.util.express.IExpressResourceLoader;
import com.ql.util.express.parse.NodeTypeManager;
import com.ql.util.express.self.combat.function.logic.*;
import com.ql.util.express.self.combat.function.math.*;

/**
 * 类描述: 仿简道云公式函数实战入口类
 *
 * @author admin
 * @version 1.0.0
 * @date 2023/11/21 15:29
 */
public class FormulaRunner extends ExpressRunner {

    public FormulaRunner() {
        super();
    }

    public FormulaRunner(boolean isPrecise, boolean isTrace) {
        super(isPrecise,isTrace);
    }

    public FormulaRunner(boolean isPrecise, boolean isStrace, NodeTypeManager nodeTypeManager) {
        super(isPrecise,isStrace,nodeTypeManager);
    }

    public FormulaRunner(boolean isPrecise, boolean isTrace, IExpressResourceLoader iExpressResourceLoader, NodeTypeManager nodeTypeManager) {
        super(isPrecise,isTrace,iExpressResourceLoader,nodeTypeManager);
    }

    @Override
    public void addSystemFunctions() {
        // ExpressRunner 的内部系统函数
        super.addSystemFunctions();
        // 扩展公式函数
        this.customFunction();
    }
    /***
     * 自定义公式函数
     */
    public void customFunction() {

        // 逻辑公式函数
        this.addLogicFunction();

        // 数学公式函数
        this.addMathFunction();
    }

    public void addLogicFunction() {
        // AND函数
        this.addFunction("AND",new AndFunction("AND"));

        // IF函数
        this.addFunction("IF",new IfFunction("IF"));

        // IFS函数
        this.addFunction("IFS",new IfsFunction("IFS"));

        // XOR函数
        this.addFunction("XOR",new XorFunction("XOR"));

        // TRUE函数
        this.addFunction("TRUE",new TrueFunction("TRUE"));

        // FALSE函数
        this.addFunction("FALSE",new FalseFunction("FALSE"));

        // NOT函数
        this.addFunction("NOT",new NotFunction("NOT"));

        // OR函数
        this.addFunction("OR",new OrFunction("OR"));
    }

    public void addMathFunction() {
        // ABS函数
        this.addFunction("ABS",new AbsFunction("ABS"));

        // AVERAGE函数
        this.addFunction("AVERAGE",new AvgFunction("AVERAGE"));

        // CEILING函数
        this.addFunction("CEILING",new CeilingFunction("CEILING"));

        // RADIANS函数
        this.addFunction("RADIANS",new RadiansFunction("RADIANS"));

        // COS函数
        this.addFunction("COS",new CosFunction("COS"));

        // COT函数
        this.addFunction("COT",new CotFunction("COT"));

        // COUNT函数
        this.addFunction("COUNT",new CountFunction("COUNT"));

        // COUNTIF函数
        this.addFunction("COUNTIF",new CountIfFunction("COUNTIF"));

        // FIXED函数
        this.addFunction("FIXED",new FixedFunction("FIXED"));

        // FLOOR函数
        this.addFunction("FLOOR",new FloorFunction("FLOOR"));

        // INT函数
        this.addFunction("INT",new IntFunction("INT"));

        // LARGE函数
        this.addFunction("LARGE",new LargeFunction("LARGE"));

        // LOG函数
        this.addFunction("LOG",new LogFunction("LOG"));

    }
}

创建测试用例

package com.ql.util.express.self.combat;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.self.combat.ext.FormulaRunner;
import org.junit.Test;

/**
 * 类描述: 实战测试类
 *
 * @author admin
 * @version 1.0.0
 * @date 2023/11/21 15:45
 */
public class CombatTest {

    @Test
    public void LOG() throws Exception{

        FormulaRunner formulaRunner = new FormulaRunner(true,true);
        // 创建上下文
        DefaultContext<String, Object> context = new DefaultContext<>();
        String express = "LOG(16,2)";
        Object object = formulaRunner.execute(express, context, null, true, true);
        System.out.println(object);

    }

}

运行结果

相关推荐

  1. C++ 仿函数

    2024-02-22 19:56:01       33 阅读
  2. excel函数公式

    2024-02-22 19:56:01       34 阅读
  3. 27.函数指针数组

    2024-02-22 19:56:01       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-22 19:56:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-22 19:56:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-22 19:56:01       87 阅读
  4. Python语言-面向对象

    2024-02-22 19:56:01       96 阅读

热门阅读

  1. oracle读写分离多数据源

    2024-02-22 19:56:01       43 阅读
  2. Spring整合Junit4

    2024-02-22 19:56:01       51 阅读
  3. 【linux系统讲解】

    2024-02-22 19:56:01       40 阅读
  4. 在做了frp的实验室服务器不同端口间传输文件

    2024-02-22 19:56:01       51 阅读
  5. python子域名收集工具

    2024-02-22 19:56:01       45 阅读
  6. C#面:怎样理解静态变量

    2024-02-22 19:56:01       42 阅读
  7. Android中自定义View时尺寸需要注意的相关事项

    2024-02-22 19:56:01       63 阅读
  8. Willem, Chtholly and Seniorious(珂朵莉树)

    2024-02-22 19:56:01       54 阅读
  9. Python截取视频帧

    2024-02-22 19:56:01       50 阅读