calculator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Calculator

计算器

Evaluate arithmetic expressions with arbitrary-precision decimal math using big.js.
使用big.js对算术表达式进行任意精度的十进制数学求值。

When to Use

适用场景

  • User asks to calculate or evaluate a math expression
  • Precise decimal arithmetic is needed (avoids floating-point errors like
    0.1 + 0.2 = 0.30000000000000004
    )
  • Expressions involve parentheses, operator precedence, or exponents
  • 用户要求计算或求值数学表达式
  • 需要精确的十进制算术(避免类似
    0.1 + 0.2 = 0.30000000000000004
    的浮点错误)
  • 表达式包含括号、运算符优先级或幂运算

Supported Operations

支持的运算

OperatorDescriptionPrecedence
+
Addition1
-
Subtraction1
*
Multiplication2
/
Division2
^
Exponent (right-associative)3
()
ParenthesesHighest
运算符描述优先级
+
加法1
-
减法1
*
乘法2
/
除法2
^
幂运算(右结合)3
()
括号最高

Usage

使用方法

bash
cd scripts
npm ci || npm install
npm run build
npm run calculate "<expression>"
bash
cd scripts
npm ci || npm install
npm run build
npm run calculate "<expression>"

Examples

示例

InputOutput
"3 + 2"
5
"10 / 4"
2.5
"2 ^ 10"
1024
"(2 + 3) * 4"
20
"1 + 4.5 * (3-6) / 5"
-1.7
"-5 + 3"
-2
"2 ^ 3 ^ 2"
512
(right-associative: 2^9)
输入输出
"3 + 2"
5
"10 / 4"
2.5
"2 ^ 10"
1024
"(2 + 3) * 4"
20
"1 + 4.5 * (3-6) / 5"
-1.7
"-5 + 3"
-2
"2 ^ 3 ^ 2"
512
(右结合:2^9)

Edge Cases

边缘情况

  • Empty expression: Throws "Empty expression" error
  • Mismatched parentheses: Throws "Mismatched parentheses" error
  • Division by zero: big.js throws an error
  • Exponent must be integer: big.js
    .pow()
    requires integer exponents
  • 空表达式:抛出"Empty expression"错误
  • 括号不匹配:抛出"Mismatched parentheses"错误
  • 除以零:big.js抛出错误
  • 幂运算指数必须为整数:big.js
    .pow()
    要求指数为整数

Limitations

局限性

  • No trigonometric functions (sin, cos, tan)
  • No variables or symbolic math
  • Exponents must be integers
  • No factorial, modulo, or bitwise operators
  • 无三角函数(sin、cos、tan)
  • 不支持变量或符号数学
  • 幂运算指数必须为整数
  • 无阶乘、取模或位运算符