Boost
arrow_drop_down
Boost.Multiprecision
M
D

本次发布

John Maddock
John Maddock
作者
Christopher Kormanyos
作者
Christopher Kormanyos
Christopher Kormanyos
维护者
Richard Thomson
Richard Thomson
贡献者 - 新
ivanpanch
贡献者 - 新
Matt Borland
Matt Borland
贡献者
Pavel P
贡献者

依赖项

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

在以下示例中,我们使用 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

全部时间

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