Scripts2026年7月7日·1 分钟阅读

math.js — Extensive Math Library for JavaScript

A comprehensive math library for JavaScript and Node.js with expression parsing, units, matrices, and big numbers.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
math.js Overview
直接安装命令
npx -y tokrepo@latest install fc0e56ce-799d-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

math.js is a comprehensive mathematics library for JavaScript and Node.js. It provides a flexible expression parser, supports a wide range of data types including complex numbers, big numbers, fractions, units, and matrices, and offers hundreds of built-in functions. It serves as the go-to math library when native JavaScript arithmetic is not enough.

What math.js Does

  • Parses and evaluates mathematical expressions from strings
  • Supports complex numbers, fractions, big numbers, and units
  • Provides matrix and linear algebra operations
  • Includes a symbolic computation engine for simplification and derivatives
  • Offers both a chained API and a functional programming style

Architecture Overview

math.js is built around a dependency injection system that allows tree-shaking unused functions. The expression parser tokenizes input into an AST, which is then compiled into an efficient evaluation function. Data types like BigNumber, Complex, Unit, and Matrix are first-class objects with operator overloading. The library supports custom function and constant registration, making it extensible for domain-specific use cases.

Self-Hosting & Configuration

  • Install via npm: npm install mathjs
  • Import the full library or use create() with specific factories for smaller bundles
  • Configure precision for big numbers via math.config({ number: 'BigNumber', precision: 64 })
  • Register custom functions with math.import({ myFunc: (x) => x * 2 })
  • Use in the browser via bundlers or a CDN script tag

Key Features

  • Expression parser with variable scope and function definitions
  • Arbitrary precision arithmetic via BigNumber support
  • Physical unit conversions (meters, joules, seconds, etc.)
  • Sparse and dense matrix types with decomposition algorithms
  • Chainable API for readable mathematical pipelines

Comparison with Similar Tools

  • decimal.js — focused on arbitrary precision decimals; math.js wraps it and adds matrices, units, and parsing
  • NumPy (Python) — the Python equivalent; math.js brings similar capabilities to JavaScript
  • algebra.js — limited to polynomial algebra; math.js covers far more ground
  • numbers.js — smaller scope with basic stats and calculus; math.js is more comprehensive

FAQ

Q: Can I use math.js for unit conversions? A: Yes. Use math.unit('5 inch').to('cm') to convert between physical units.

Q: Does math.js support big number arithmetic? A: Yes. Configure the library to use BigNumber as the default number type for arbitrary precision calculations.

Q: How do I evaluate user-supplied expressions safely? A: Use a limited parser scope and avoid exposing dangerous functions. The expression parser does not execute arbitrary JavaScript.

Q: Can I tree-shake unused functions? A: Yes. Use create() with only the factories you need to produce a smaller bundle.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产