# 单位换算软件

单位换算软件是任何可在不同计量单位之间换算测量值的程序、计算器或库，其更完整的形式还会在计算过程中追踪量的物理量纲，从而自动捕获不兼容的运算。这一类别涵盖在线换算器、GNU units 等命令行工具，以及 Python、Julia、Haskell、TypeScript 中的量纲感知计算库和商业数学系统。与印刷的换算表不同，这些工具可以在任意量纲表达式中追踪单位以确保量纲一致性，并且能处理简单乘法因子无法表达的换算，例如摄氏度到华氏度。<sup>[1](https://manpages.debian.org/bookworm/units/units.1.en.html)</sup><sup> • </sup><sup>[2](https://docs.thi.ng/umbrella/units/index.html)</sup>

| 关键事实 | 详细说明 |
|---|---|
| 内部表示 | @thi.ng/units 等单位库将单位编码为七个国际单位制基本量纲上的指数向量，外加一个比例因子和零点偏移<sup>[2](https://docs.thi.ng/umbrella/units/index.html)</sup> |
| 基本单位上限 | 一个单位系统最多有 7 个基本单位，它们必须在质量、时间、长度、电流、发光强度、物质的量和温度这些维度上量纲独立<sup>[3](https://www.mathworks.com/help/symbolic/units-conversion.html)</sup> |
| 仿射单位 | 温标需要因子和偏移量；各库要么通过开尔文中转，要么直接应用仿射变换，要么禁止用 °C/°F 表示温度差<sup>[4](https://www.quantacalculator.com/conversion/unit-converter)</sup><sup> • </sup><sup>[5](https://github.com/JuliaPhysics/Unitful.jl/blob/master/docs/src/index.md)</sup> |
| 错误检查 | 运行时库抛出类型化错误，如 DimensionError 或“不兼容”消息；Haskell 库在编译时拒绝量纲不匹配<sup>[6](https://pypi.org/project/unitful/)</sup><sup> • </sup><sup>[1](https://manpages.debian.org/bookworm/units/units.1.en.html)</sup><sup> • </sup><sup>[7](https://github.com/AliceRixte/convert-units)</sup> |
| 典型精度 | 有文档记录的库报告：输入值不超过 1e15 时线性换算相对误差低于 1e-12，温度误差低于 1e-9 K<sup>[8](https://github.com/biggora/unit-converter)</sup> |
| 精度模型 | 引擎要么使用 IEEE 754 双精度（约 15–17 位有效数字），要么采用更高的十进制工作精度（一个有文档记录的换算器使用 20 位有效数字）<sup>[9](https://convertitive.com/methodology/units/)</sup><sup> • </sup><sup>[4](https://www.quantacalculator.com/conversion/unit-converter)</sup> |
| 库的规模 | 有文档记录的包从 13 个类别 100 多个单位，到 20 个类别 200 个单位，以及约 170 个预定义单位和常量不等<sup>[10](https://registry.npmjs.org/all-units)</sup><sup> • </sup><sup>[11](https://registry.npmjs.org/unitfyi)</sup><sup> • </sup><sup>[2](https://docs.thi.ng/umbrella/units/index.html)</sup> |

## 量纲感知计算的工作原理

量纲感知库不会把“5 米”当作数字 5 来处理。它为每个值附加一个单位定义，在最显式的设计中，该定义包含三部分：基本量纲上的指数向量、比例因子和零点偏移。指数向量就是量纲签名。TypeScript 库 @thi.ng/units 将每个单位表示为国际单位制基本单位量纲（kg、m、s、A、K、mol、cd）的七维指数向量，外加每个单位的比例因子和零点偏移。<sup>[2](https://docs.thi.ng/umbrella/units/index.html)</sup>

指数向量正是量纲检查得以实现的基础。GNU units 从用户角度描述了同样的机制：你可以构造任意复杂的量纲数学表达式，包括求和、乘积、商、幂甚至量纲的根，程序会在整个表达式中追踪单位以确保量纲一致性。<sup>[1](https://manpages.debian.org/bookworm/units/units.1.en.html)</sup>

换算如何进行取决于因子表的组织方式。轮辐式方法为每个单位存储一个相对于规范基准的精确换算因子，并在运行时通过该基准计算任意一对单位的换算。换算网站 Convertitive 将每个单位存储为相对于国际计量局《国际单位制手册》（第 9 版，2019 年）所定义的七个国际单位制基本单位之一的单一换算因子，并通过该规范基准计算成对换算，而不是存储 N² 个成对因子，从而使厘米 → 英寸和英寸 → 厘米的往返换算精确无误。<sup>[9](https://convertitive.com/methodology/units/)</sup>

单位系统约束哪些单位可以充当基准。MathWorks 将单位系统定义为基本单位加导出单位，其中基本单位必须在质量、时间、长度、电流、发光强度、物质的量和温度上量纲独立，一个系统最多 7 个基本单位；任何单位——包括牛顿或瓦特——只要满足独立性要求都可以充当基本单位。导出单位必须能表示为该系统基本单位的幂的乘积，可用的系统包括国际单位制、厘米-克-秒制和美制，并支持自定义系统。<sup>[3](https://www.mathworks.com/help/symbolic/units-conversion.html)</sup> Wolfram 的 UnitConvert 接受整个单位系统作为换算目标，包括“SIBase”、“SI”、“Imperial”、“Conventional”和“Metric”，让用户可以将表达式转换为“国际单位制认为方便的任何形式”，而不必指定单一的目标单位。<sup>[12](https://reference.wolfram.com/language/ref/UnitConvert.html.en)</sup> SymPy 的 convert_to(expr, target_units, unit_system='SI') 同样在量纲兼容时将表达式重写为以目标单位为因子的形式。<sup>[13](https://docs.sympy.org/dev/modules/physics/units/quantities.html)</sup>

## 处理仿射单位：温度

对于共享零点的单位，乘以因子就足够了，但对温度则不行。摄氏度和华氏度标度是仿射的而非线性的，因为每个标度的零点都偏离绝对零度，所以换算同时涉及因子和偏移量。<sup>[9](https://convertitive.com/methodology/units/)</sup>

有文档记录的工具采取三种可区分的方法。第一种是通过绝对标度中转：Quanta 换算器先将输入转换为开尔文，再从开尔文转换为目标单位，这保证了往返换算（C → F → C）精确返回原值，并避免了负零错误。<sup>[4](https://www.quantacalculator.com/conversion/unit-converter)</sup> 第二种是带差值限制的直接仿射变换：Unitful.jl 在考虑相对标度的仿射变换下换算 °C 或 °F 的量，但禁止这些单位表示温度差（1 °C − 0 °C 等于 1 K），以避免可能导致错误结果的歧义。<sup>[5](https://github.com/JuliaPhysics/Unitful.jl/blob/master/docs/src/index.md)</sup> 第三种是明确区分两个概念：Wolfram 的 UnitConvert 可以分别换算温度和温度差，将温度解释为相对于绝对零度的差值。<sup>[12](https://reference.wolfram.com/language/ref/UnitConvert.html.en)</sup> Python 库 unitful 通过其 .to(unit) 方法在偏移温标（degC、degF、K）之间换算。<sup>[6](https://pypi.org/project/unitful/)</sup>

这些策略并非仅仅出于美观。一个将 °C 视为相对于开尔文的普通因子 1 的库会静默地计算出错误的温度差，这就是为什么 Unitful.jl 将其限制表述为防止错误结果，而非风格选择。<sup>[5](https://github.com/JuliaPhysics/Unitful.jl/blob/master/docs/src/index.md)</sup>

## 量纲检查与错误预防

检查可以在运行时或编译时进行。

运行时检查在程序执行时验证量纲。Python 库 unitful 将单位附加到数字上，在量纲不匹配时于运行时抛出 DimensionError，而不是产生静默的错误结果。<sup>[6](https://pypi.org/project/unitful/)</sup> 它还提供 @requires 和 @returns 装饰器，对函数参数和返回值强制执行量纲约束。<sup>[6](https://pypi.org/project/unitful/)</sup> PicoUnits 是一个单依赖（NumPy）的 Python 包，通过 @unit_validator 装饰器在运行时进行量纲分析；其文档记录的失败消息为“DimensionError：'calculate_voltage' 返回了 kg·m²·s⁻³，预期为 kg·m²·s⁻³·A⁻¹”。<sup>[14](https://github.com/wgbowley/PicoUnits)</sup> GNU units 在交互层面会打印一条消息指示单位不兼容，并在输入不兼容类型时显示每个单位的约简形式。<sup>[1](https://manpages.debian.org/bookworm/units/units.1.en.html)</sup> Wolfram 的 UnitConvert 对不兼容的指定（例如不同量纲之间的换算）返回 $Failed。<sup>[12](https://reference.wolfram.com/language/ref/UnitConvert.html.en)</sup>

编译时检查将同样的逻辑移入类型系统。Haskell 库 convert-units 在编译时静态检查单位量纲，产生诸如“无法将单位 'min' 换算为单位 'm'，因为两者量纲不匹配”的错误，并声称通过内联实现与手动乘法相当的换算速度，因此安全性不以运行时性能为代价。<sup>[7](https://github.com/AliceRixte/convert-units)</sup> Unitful.jl 走中间路线：它旨在不仅支持国际单位制，还支持任何其他单位系统，同时最小化或在某些情况下消除单位的运行时开销。<sup>[5](https://github.com/JuliaPhysics/Unitful.jl/blob/master/docs/src/index.md)</sup>

## 数据一览

证据集中记录的规模和精度数据可以让人了解现成换算代码的表现。

- **库的覆盖范围。** @thi.ng/units 附带约 170 个预定义单位和常量，并提供一个包含类 Lisp DSL 的命令行计算器。<sup>[2](https://docs.thi.ng/umbrella/units/index.html)</sup> unitfyi 是一个零依赖的 TypeScript 引擎，覆盖 20 个计量类别的 200 个单位，包括 20 个长度单位、4 个温标和 16 个数据存储单位。<sup>[11](https://registry.npmjs.org/unitfyi)</sup> all-units 提供 13 个类别 100 多个单位，具有链式 API（convert(100).from('cm').to('m')）和零运行时依赖。<sup>[10](https://registry.npmjs.org/all-units)</sup>
- **精度。** biggora/unit-converter 是一个 TypeScript 库，将每个英制系数锚定到 NIST SP 811 §B.8/§B.9，报告输入值不超过 1e15 时线性换算相对误差低于 1e-12，浮点偏移运算的温度绝对误差低于 1e-9 K。<sup>[8](https://github.com/biggora/unit-converter)</sup>
- **精度模型。** Convertitive 以 IEEE 754 双精度运行每次换算，提供约 15–17 位有效十进制数字，显示舍入（长度、重量和体积保留 6 位有效数字；温度保留 2 位小数）是唯一的精度损失。<sup>[9](https://convertitive.com/methodology/units/)</sup> Quanta 则以二十位有效数字的十进制工作精度进行计算，因此 1 英里 = 1609.344 米和 1 美制加仑 = 3.785411784 升等定义值是精确的而非浮点近似。<sup>[4](https://www.quantacalculator.com/conversion/unit-converter)</sup>
- **数字在哪里丢失。** 即使因子精确，极端换算比也会造成问题：在 biggora 库中将光年换算为英寸（比率约为 3.7e17）会丢失 1–2 个最低有效数字。<sup>[8](https://github.com/biggora/unit-converter)</sup>

## 工具全景

这些工具大致分为四组，以接口和检查发生的位置相区分。

**在线计算器**面向一次性换算。Quanta 的因子基于 NIST SP 811 和 BIPM《国际单位制手册》（第 9 版），并在方法学页面上记录了其精度和温度中转选择。<sup>[4](https://www.quantacalculator.com/conversion/unit-converter)</sup> Convertitive 同样公布了其因子来源，包括 NIST SP 811 以及 1959 年国际码磅协定（用于英制和美制单位）。<sup>[9](https://convertitive.com/methodology/units/)</sup>

**命令行和交互式工具**服务于终端前的技术用户。GNU units 处理乘法比例变化和非线性换算（如华氏度到摄氏度），并能与单位之和互相换算，例如米与英尺加英寸之间的换算。<sup>[1](https://manpages.debian.org/bookworm/units/units.1.en.html)</sup>

**库**将单位感知嵌入程序。在 Python 中，unitful（v1.1.0）提供运行时 DimensionError 检查、通过 QuantityArray 集成 NumPy（其 ufunc 和归约会保留单位）、通过 define_unit 和 new_dimension 进行运行时扩展、JSON 往返序列化以及 Unicode/LaTeX/HTML 输出。<sup>[6](https://pypi.org/project/unitful/)</sup> PicoUnits 增加了可插拔的单位系统（“Unit Frames”）、.uiv 配置 DSL，以及对实数、复数和基于数组的值的支持。<sup>[14](https://github.com/wgbowley/PicoUnits)</sup> unitpy 直接实现 NIST 特别出版物 811（《国际单位制指南》）作为其单位定义。<sup>[15](https://github.com/Mirochill/unitpy)</sup> SymPy 通过 convert_to 进行符号换算。<sup>[13](https://docs.sympy.org/dev/modules/physics/units/quantities.html)</sup> 在 Julia 中，Unitful.jl 以最小的运行时开销支持任意单位系统。<sup>[5](https://github.com/JuliaPhysics/Unitful.jl/blob/master/docs/src/index.md)</sup> 在 TypeScript 中，@thi.ng/units、biggora/unit-converter、unitfyi 和 all-units 涵盖了从量纲计算到普通换算表的范围。<sup>[2](https://docs.thi.ng/umbrella/units/index.html)</sup><sup> • </sup><sup>[8](https://github.com/biggora/unit-converter)</sup><sup> • </sup><sup>[11](https://registry.npmjs.org/unitfyi)</sup><sup> • </sup><sup>[10](https://registry.npmjs.org/all-units)</sup> 在 Haskell 中，convert-units 执行编译时检查。<sup>[7](https://github.com/AliceRixte/convert-units)</sup>

**商业数学系统**将换算与符号计算相结合。Wolfram 的 UnitConvert 于 2012 年（9.0 版）引入，并于 2022 年（13.2）和 2024 年（Wolfram Language 14.0）更新，可换算为命名单位系统，货币换算需要互联网连接。<sup>[12](https://reference.wolfram.com/language/ref/UnitConvert.html.en)</sup> MATLAB 的 Symbolic Math Toolbox 提供含基本单位和导出单位的单位系统，并在换算到某个系统时自动选择方便的导出单位。<sup>[3](https://www.mathworks.com/help/symbolic/units-conversion.html)</sup>

## References

1. [units(1) — GNU units manpage (Debian bookworm)](https://manpages.debian.org/bookworm/units/units.1.en.html)
2. [@thi.ng/units documentation](https://docs.thi.ng/umbrella/units/index.html)
3. [Unit Conversions and Unit Systems — MATLAB Symbolic Math Toolbox](https://www.mathworks.com/help/symbolic/units-conversion.html)
4. [Unit Converter — Quanta online calculator](https://www.quantacalculator.com/conversion/unit-converter)
5. [Unitful.jl documentation (JuliaPhysics)](https://github.com/JuliaPhysics/Unitful.jl/blob/master/docs/src/index.md)
6. [unitful v1.1.0 — PyPI](https://pypi.org/project/unitful/)
7. [AliceRixte/convert-units (Haskell)](https://github.com/AliceRixte/convert-units)
8. [biggora/unit-converter (TypeScript)](https://github.com/biggora/unit-converter)
9. [Units methodology | Convertitive](https://convertitive.com/methodology/units/)
10. [all-units — npm registry](https://registry.npmjs.org/all-units)
11. [unitfyi — npm registry](https://registry.npmjs.org/unitfyi)
12. [UnitConvert — Wolfram Documentation](https://reference.wolfram.com/language/ref/UnitConvert.html.en)
13. [Physical quantities — SymPy documentation](https://docs.sympy.org/dev/modules/physics/units/quantities.html)
14. [PicoUnits — lightweight dimensional analysis library for Python](https://github.com/wgbowley/PicoUnits)
15. [unitpy — Python units package](https://github.com/Mirochill/unitpy)

---
*Initially written Sep 17, 2026 · Edited: Oct 11, 2026; Oct 15, 2026*

Topic: 物质世界与数学 › 测量与时间 › 计量单位与单位制 › Unit conversion and dimensional analysis › Conversion tools and unit-aware computation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/unit-conversion-software

English original: https://www.edgepedia.org/unit-conversion-software
