Enum Class ArithmeticMode

java.lang.Object
java.lang.Enum<ArithmeticMode>
cloud.opencode.base.expression.ArithmeticMode
All Implemented Interfaces:
Serializable, Comparable<ArithmeticMode>, Constable

public enum ArithmeticMode extends Enum<ArithmeticMode>
Arithmetic Precision Mode 算术精度模式

Defines the arithmetic precision mode used during expression evaluation. Controls whether numeric operations use standard Java arithmetic or BigDecimal for arbitrary-precision calculations.

定义表达式求值期间使用的算术精度模式。 控制数值运算使用标准Java算术还是 BigDecimal 进行任意精度计算。

Features | 主要功能:

  • STANDARD - Default mode using int/long/double arithmetic for maximum performance - 默认模式,使用int/long/double算术以获得最大性能
  • BIG_DECIMAL - Precision mode using BigDecimal for all numeric operations, ideal for financial calculations - 精确模式,所有数值运算使用BigDecimal,适用于金融计算

Usage Examples | 使用示例:

// Standard mode (default): fast int/long/double arithmetic
StandardContext ctx = StandardContext.builder()
    .arithmeticMode(ArithmeticMode.STANDARD)
    .build();
Object result = OpenExpression.evaluate("0.1 + 0.2", ctx);
// result = 0.30000000000000004 (double precision)

// BigDecimal mode: exact arithmetic for financial use cases
StandardContext ctx = StandardContext.builder()
    .arithmeticMode(ArithmeticMode.BIG_DECIMAL)
    .build();
Object result = OpenExpression.evaluate("0.1 + 0.2", ctx);
// result = 0.3 (exact BigDecimal)

// Check current mode
ArithmeticMode mode = ArithmeticMode.STANDARD;
if (mode == ArithmeticMode.BIG_DECIMAL) {
    // Use BigDecimal operations
}

Security | 安全性:

  • Thread-safe: Yes, enum constants are inherently immutable and thread-safe - 线程安全: 是,枚举常量本质上是不可变和线程安全的
  • Null-safe: Enum values cannot be null - 空值安全: 枚举值不能为null

Performance | 性能:

  • STANDARD: Fastest, uses primitive arithmetic - 最快,使用原始算术
  • BIG_DECIMAL: Slower (3-10x) but exact - 较慢(3-10倍)但精确
Since:
JDK 25, opencode-base-expression V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Enum Constant Details

    • STANDARD

      public static final ArithmeticMode STANDARD
      Standard arithmetic mode 标准算术模式

      Uses Java's default int, long, and double arithmetic. Offers the best performance but may exhibit floating-point rounding errors for decimal values.

      使用Java默认的int、long和double算术。提供最佳性能, 但对于十进制值可能出现浮点舍入误差。

    • BIG_DECIMAL

      public static final ArithmeticMode BIG_DECIMAL
      BigDecimal arithmetic mode BigDecimal算术模式

      Promotes all numeric operands to BigDecimal before performing arithmetic operations. Provides exact decimal arithmetic suitable for financial and scientific calculations where precision is critical.

      在执行算术运算前将所有数值操作数提升为 BigDecimal。 提供精确的十进制算术,适用于精度至关重要的金融和科学计算。

  • Method Details

    • values

      public static ArithmeticMode[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static ArithmeticMode valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null