用于浮点、整数和有理数算术的扩展精度算术类型。
本次发布
依赖项
Boost Multiprecision 库
| 主分支 | 开发分支 | |
|---|---|---|
| Drone | ||
| Github Actions | ||
| Codecov |
Boost.Multiprecision 是一个 C++ 库,它提供整数、有理数、浮点数、复数和区间数类型,这些类型的范围和精度都比语言内置的普通类型要高。
语言兼容性
Boost.Multiprecision需要一个兼容的 C++14 编译器。- 它兼容 C++14、17、20、23 及更高版本。
Boost.Multiprecision 中的大数类型可以与多种基本数学运算、初等超越函数以及 Boost.Math 中的函数配合使用。Multiprecision 类型也与 C++ 的内置类型进行互操作。大数类型遵循明确定义的转换规则。这使得 Boost.Multiprecision 可以用于所有需要扩展范围和精度的整数、有理数和浮点类型涉及的数学计算。
Multiprecision 由一个通用的接口来处理大数数学运算,以及一系列大数后端。这些后端包括对 GMP、MPFR、MPIR 和 TomMath 的接口,以及 Multiprecision 自有的 Boost 许可的、仅头文件的整数、有理数、浮点数和复数浮点数后端集合。
此外,还可以创建用户定义的后端并与 Multiprecision 的接口一起使用,前提是类实现符合必要的概念。
根据多精度类型,精度可以是任意大(仅受可用内存限制),也可以是编译时固定的(例如 $50$ 或 $100$ 位十进制数字),或者是在运行时由成员函数控制的可变精度。在配置带有后端的 number 类型时,可以启用或禁用表达式模板。大多数多精度类型默认启用表达式模板。这通常比使用未配置表达式模板的类型性能更好。
完整文档可在 boost.org 上找到。
一个实用、全面、富有启发性、清晰且非常有用的关于 Multiprecision 用法的视频可以在 这里找到。
使用 Multiprecision
在以下示例中,我们使用 Multiprecision 的 Boost 许可的二进制浮点后端类型 cpp_bin_float 来计算 ${\sim}100$ 位十进制数字的
$$\sqrt{\pi} = \Gamma \left( \frac{1}{2} \right)~{\approx}~1.772453850905516027298{\ldots}\text{,}$$
其中我们还观察到 Multiprecision 可以与 Boost.Math 无缝互操作。
// ------------------------------------------------------------------------------
// Copyright Christopher Kormanyos 2024 - 2025.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at https://boost.ac.cn/LICENSE_1_0.txt)
//
#include <boost/math/special_functions/gamma.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <iomanip>
#include <iostream>
#include <sstream>
auto main() -> int
{
// Configure a multiprecision binary floating-point type with approximately
// 100 decimal digits of precision and expression templates enabled.
// Note that the popular type cpp_bin_float_100 has already been preconfigured
// and aliased in the multiprecision headers.
using big_float_type = boost::multiprecision::cpp_bin_float_100;
// In the next few lines, compute and compare sqrt(pi) with tgamma(1/2)
// using the 100-digit multiprecision type.
const big_float_type sqrt_pi { sqrt(boost::math::constants::pi<big_float_type>()) };
const big_float_type half { big_float_type(1) / 2 };
const big_float_type gamma_half { boost::math::tgamma(half) };
std::stringstream strm { };
// 1.772453850905516027298167483341145182797549456122387128213807789852911284591032181374950656738544665
strm << std::setprecision(std::numeric_limits<big_float_type>::digits10)
<< "sqrt_pi : "
<< sqrt_pi
<< "\ngamma_half: "
<< gamma_half;
std::cout << strm.str() << std::endl;
}
独立使用
定义 BOOST_MP_STANDALONE 允许 Boost.Multiprecision 独立使用,唯一的依赖是 Boost.Config。我们 此页面上的包 已包含 Boost.Config 的副本。因此,无需其他下载。
在此模式下,部分功能会受到限制。如果某个特定功能被独立模式禁用,static_assert 消息会发出警报。 Boost.Math 独立模式是兼容的,如果您需要为浮点类型使用特殊函数,建议使用此模式。
支持、错误和功能请求
可以通过 Gitub 问题跟踪器 报告错误和功能请求(请参阅 未解决的问题和 已关闭的问题)。
您可以通过 拉取请求提交您的更改。
没有专门针对 Boost Multiprecision 的邮件列表,但您可以使用通用的 Boost 邮件列表,并使用标签 [multiprecision]。
开发
克隆整个 boost 项目,其中包含各个 Boost 项目作为子模块(参见 boost+git 文档)。
git clone https://github.com/boostorg/boost
cd boost
git submodule update --init
Boost.Multiprecision 库位于 libs/multiprecision/。
运行测试
首先,通过在 boost 目录的根目录下运行 bootstrap.sh 来构建 b2 引擎。这将在 project-config.jam 中生成 b2 配置。
./bootstrap.sh
现在,确保您位于 libs/multiprecision/test。您可以运行 Jamfile.v2 中列出的所有测试,或者运行单个测试。
../../../b2 <- run all tests
../../../b2 test_complex <- single test