Boost
arrow_drop_down
Boost.Multiprecision
M
D

本次发布

John Maddock
John Maddock
作者
Christopher Kormanyos
作者
Christopher Kormanyos
Christopher Kormanyos
维护者
Syed Fahad
贡献者 - 新
Janek Kozicki
贡献者 - 新
Fahad Syed
贡献者 - 新
Pavel P
贡献者 - 新
Matt Borland
Matt Borland
贡献者
Rene Rivera
Rene Rivera
贡献者

依赖项

Boost Multiprecision 库

主分支 开发分支
Drone Build Status Build Status
Github Actions Build Status Build Status
Codecov codecov 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

全部时间

Madhur Chauhan
Madhur Chauhan
贡献者
Paul A. Bristow
Paul A. Bristow
贡献者
NAThompson
NAThompson
贡献者
Iskandarov Lev Andreevich
贡献者
Nick
Nick
贡献者
Marshall Clow
Marshall Clow
贡献者
Edward Diener
Edward Diener
贡献者
Andrey Semashev
Andrey Semashev
贡献者
Beman Dawes
Beman Dawes
贡献者
Ryan
Ryan
贡献者
sdarwin
sdarwin
贡献者
Adam Wulkiewicz
Adam Wulkiewicz
贡献者
Jürgen Hunold
Jürgen Hunold
贡献者
Glen Fernandes
Glen Fernandes
贡献者
Raffi Enficiaud
Raffi Enficiaud
贡献者
Peter Dimov
Peter Dimov
贡献者
Romain Geissler
Romain Geissler
贡献者
emfrias
emfrias
贡献者
Mikhail Komarov
Mikhail Komarov
贡献者
Paolo Galeone
Paolo Galeone
贡献者
Tinko Bartels
Tinko Bartels
贡献者
AntonBikineev
AntonBikineev
贡献者
Stefan Vigerske
Stefan Vigerske
贡献者
Cosmin Boaca
贡献者
Alexandre Hamez
Alexandre Hamez
贡献者
Pavel I. Kryukov
Pavel I. Kryukov
贡献者
stanislav.mikov
贡献者
Daniel James
Daniel James
贡献者
Kefu Chai
Kefu Chai
贡献者
Alexander Grund
Alexander Grund
贡献者
Thomas Barbier
Thomas Barbier
贡献者
Marcel Raad
Marcel Raad
贡献者
E Kawashima
E Kawashima
贡献者
Hans Dembinski
Hans Dembinski
贡献者
Giovanni Mascellani
Giovanni Mascellani
贡献者
zerotypos-found
zerotypos-found
贡献者
Sergey Zubkov
Sergey Zubkov
贡献者
acc987
acc987
贡献者
Sergei Fedorov
Sergei Fedorov
贡献者
Jay Freeman (saurik)
Jay Freeman (saurik)
贡献者
anilmuthigi
anilmuthigi
贡献者
Shivam Gupta
Shivam Gupta
贡献者
Mark Geck
Mark Geck
贡献者
Marcin Niestroj
Marcin Niestroj
贡献者
Stefan Dragnev
Stefan Dragnev
贡献者
Claudio DeSouza
Claudio DeSouza
贡献者
Mario Sanchez Prada
Mario Sanchez Prada
贡献者
warren
warren
贡献者
Laurent Rineau
Laurent Rineau
贡献者
Nuno Nobre
Nuno Nobre
贡献者
Brian Wignall
Brian Wignall
贡献者
Konstantin Khlebnikov
贡献者