用于浮点、整数和有理数算术的扩展精度算术类型。
本次发布
依赖项
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 的 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
的独立模式是兼容的,如果您需要浮点类型的特殊函数,则推荐使用它。
支持、错误和功能请求
可以通过 GitHub 问题跟踪器 报告 bug 和功能请求(参见 未解决的问题 和 已解决的问题)。
您可以通过 pull request 提交您的更改。
没有专门针对 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