Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 7, 2026·3 min de lecture

math.js — Extensive Math Library for JavaScript

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

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
math.js Overview
Commande d'installation directe
npx -y tokrepo@latest install fc0e56ce-799d-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en 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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires