跳到主要内容

8. numerics (数值计算)

提供数值计算相关的组件。头文件: <cmath>, <complex>, <random>, <bitset>, <valarray>。

8.1 cmath (数学函数)​


floating_point abs(floating_point x)

绝对值。

  • 输入: x
  • 输出: 同类型

floating_point acos/asin/atan(arithmetic x)

反三角函数。

  • 输入: x
  • 输出: floating_point

floating_point atan2(arithmetic y, arithmetic x)

双参数反正切。

  • 输入: y, x
  • 输出: floating_point

floating_point ceil/floor(arithmetic x)

向上/向下取整。

  • 输入: x
  • 输出: floating_point

floating_point cos/sin/tan(arithmetic x)

三角函数。

  • 输入: x(弧度)
  • 输出: floating_point

floating_point cosh/sinh/tanh(arithmetic x)

双曲函数。

  • 输入: x
  • 输出: floating_point

floating_point exp(arithmetic x)

e 的幂。

  • 输入: x
  • 输出: floating_point

floating_point log/log10(arithmetic x)

自然/常用对数。

  • 输入: x
  • 输出: floating_point

floating_point pow(arithmetic x, arithmetic y)

幂运算。

  • 输入: x — 底数; y — 指数
  • 输出: floating_point

floating_point sqrt(arithmetic x)

平方根。

  • 输入: x
  • 输出: floating_point

floating_point fmod(arithmetic x, arithmetic y)

浮点取模。

  • 输入: x, y
  • 输出: floating_point

bool isfinite/isinf/isnan/isnormal(arithmetic x)

浮点状态检查。

  • 输入: x
  • 输出: bool
  • 注意: C++11

int fpclassify(arithmetic x)

浮点分类。

  • 输入: x
  • 输出: int

floating_point round/trunc/nearbyint/rint(arithmetic x)

各种取整。

  • 输入: x
  • 输出: floating_point

long lround/lrint(arithmetic x)

取整到 long。

  • 输入: x
  • 输出: long

floating_point hypot(arithmetic x, arithmetic y)

直角三角形斜边。

  • 输入: x, y
  • 输出: floating_point
  • 注意: 避免溢出

floating_point fma(arithmetic x, arithmetic y, arithmetic z)

融合乘加 (x*y+z)。

  • 输入: x, y, z
  • 输出: floating_point
  • 注意: 中间结果不取整

8.2 complex (复数)​


template <class T> class complex

复数类。

  • 输入: T — 浮点类型
  • 输出: 复数对象

complex(const T& re = T(), const T& im = T())

构造复数。

  • 输入: re — 实部; im — 虚部
  • 输出: 复数对象

T real() const / T imag() const

获取实部/虚部。

  • 输入: 无
  • 输出: T

T abs(const complex<T>& x)

模。

  • 输入: x
  • 输出: T

T arg(const complex<T>& x)

辐角。

  • 输入: x
  • 输出: T — 弧度

T norm(const complex<T>& x)

模的平方。

  • 输入: x
  • 输出: T

complex<T> conj(const complex<T>& x)

共轭。

  • 输入: x
  • 输出: complex<T>

complex<T> polar(const T& rho, const T& theta = T())

极坐标构造。

  • 输入: rho — 模; theta — 辐角
  • 输出: complex<T>

complex<T> exp/log/sin/cos/tan/sqrt(const complex<T>& x)

复数超越函数。

  • 输入: x
  • 输出: complex<T>

complex<T> pow(const complex<T>& x, const complex<T>& y)

复数幂。

  • 输入: x, y
  • 输出: complex<T>

8.3 random (随机数)​


class random_device

非确定性随机数源。

  • 输入: 无
  • 输出: 真随机数
  • 注意: entropy() 可能返回 0

result_type operator()()

生成随机数。

  • 输入: 无
  • 输出: unsigned int

double entropy() const noexcept

获取熵估计。

  • 输入: 无
  • 输出: double

using mt19937

32 位梅森旋转。

  • 输入: 无
  • 输出: 类型别名
  • 注意: 周期 2^19937-1

using mt19937_64

64 位梅森旋转。

  • 输入: 无
  • 输出: 类型别名

template <class IntType = int> class uniform_int_distribution

均匀整数分布。

  • 输入: IntType
  • 输出: [a, b] 闭区间均匀随机整数

explicit uniform_int_distribution(IntType a = 0, IntType b = numeric_limits<IntType>::max())

构造。

  • 输入: a — 下限; b — 上限
  • 输出: 分布对象
  • 注意: 闭区间 [a, b]

template <class RealType = double> class uniform_real_distribution

均匀实数分布。

  • 输入: RealType
  • 输出: [a, b) 均匀随机实数

class bernoulli_distribution

伯努利分布。

  • 输入: p — 概率
  • 输出: bool

class normal_distribution

正态分布。

  • 输入: mean — 均值; stddev — 标准差
  • 输出: 浮点结果

class exponential_distribution

指数分布。

  • 输入: lambda — 率参数
  • 输出: 浮点结果

class poisson_distribution

泊松分布。

  • 输入: mean — 均值
  • 输出: 整数结果

8.4 bitset (位集合)​


template <size_t N> class bitset

固定位数集合。

  • 输入: N
  • 输出: bitset 对象

bitset& set() noexcept

全部置 1。

  • 输入: 无
  • 输出: *this

bitset& reset() noexcept

全部置 0。

  • 输入: 无
  • 输出: *this

bitset& flip() noexcept

全部翻转。

  • 输入: 无
  • 输出: *this

bool all() const noexcept

是否全 1。

  • 输入: 无
  • 输出: bool

bool any() const noexcept

是否有 1。

  • 输入: 无
  • 输出: bool

bool none() const noexcept

是否全 0。

  • 输入: 无
  • 输出: bool

size_t count() const noexcept

统计 1 的个数。

  • 输入: 无
  • 输出: size_t

unsigned long to_ulong() const

转 unsigned long。

  • 输入: 无
  • 输出: unsigned long
  • 注意: 溢出抛异常

string to_string(charT zero = '0', charT one = '1') const

转字符串。

  • 输入: zero, one
  • 输出: string