std::expint, std::expintf, std::expintl
来自cppreference.com
| 在标头 <cmath> 定义
|
||
| (1) | ||
| (C++17 起) (C++23 前) |
||
| |
(C++23 起) | |
| |
(2) | (C++17 起) |
| |
(3) | (C++17 起) |
| 在标头 <cmath> 定义
|
||
| |
(A) | (C++17 起) |
A) 为所有整数类型提供额外重载,将它们当做
double。参数
| num | - | 浮点数或整数 |
返回值
如果没有发生错误,那么返回 num 的指数积分的值,即
-num
| e-t |
| t |
。
错误处理
可能报告 math_errhandling 中指定的错误。
- 如果实参是 NaN,那么返回 NaN 且不报告定义域错误
- 如果实参是 ±0,那么返回 -∞
注解
不支持 C++17,但支持 ISO 29124:2010 的实现在定义了 __STDCPP_MATH_SPEC_FUNCS__ 为至少 201003L 的值,且用户在包含任何标准库头文件前定义了 __STDCPP_WANT_MATH_SPEC_FUNCS__ 时也会提供此函数。
不支持 ISO 29124:2010 但支持 TR 19768:2007 (TR1) 的实现也会在标头 tr1/cmath 及命名空间 std::tr1 中提供此函数。
此函数的一种实现参考 boost.math。
额外重载不需要严格以 (A) 的形式提供。它们只需要能够对它们的整数类型实参 num 确保 std::expint(num) 和 std::expint(static_cast<double>(num)) 的效果相同即可。
示例
运行此代码
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
template<int Height = 5, int BarWidth = 1, int Padding = 1, int Offset = 0, class Seq>
void draw_vbars(Seq&& s, const bool DrawMinMax = true)
{
static_assert(0 < Height and 0 < BarWidth and 0 <= Padding and 0 <= Offset);
auto cout_n = [](auto&& v, int n = 1)
{
while (n-- > 0)
std::cout << v;
};
const auto [min, max] = std::minmax_element(std::cbegin(s), std::cend(s));
std::vector<std::div_t> qr;
for (typedef decltype(*std::cbegin(s)) V; V e : s)
qr.push_back(std::div(std::lerp(V(0), 8 * Height,
(e - *min) / (*max - *min)), 8));
for (auto h{Height}; h-- > 0; cout_n('\n'))
{
cout_n(' ', Offset);
for (auto dv : qr)
{
const auto q{dv.quot}, r{dv.rem};
unsigned char d[]{0xe2, 0x96, 0x88, 0}; // 完整块:'█'
q < h ? d[0] = ' ', d[1] = 0 : q == h ? d[2] -= (7 - r) : 0;
cout_n(d, BarWidth), cout_n(' ', Padding);
}
if (DrawMinMax && Height > 1)
Height - 1 == h ? std::cout << "┬ " << *max:
h ? std::cout << "│ "
: std::cout << "┴ " << *min;
}
}
int main()
{
std::cout << "Ei(0) = " << std::expint(0) << '\n'
<< "Ei(1) = " << std::expint(1) << '\n'
<< "龚帕兹常数 = " << -std::exp(1) * std::expint(-1) << '\n';
std::vector<float> v;
for (float x{1.f}; x < 8.8f; x += 0.3565f)
v.push_back(std::expint(x));
draw_vbars<9, 1, 1>(v);
}
输出:
Ei(0) = -inf
Ei(1) = 1.89512
龚帕兹常数 = 0.596347
█ ┬ 666.505
█ │
▆ █ │
█ █ │
█ █ █ │
▆ █ █ █ │
▁ ▆ █ █ █ █ │
▂ ▅ █ █ █ █ █ █ │
▁ ▁ ▁ ▁ ▁ ▁ ▁ ▂ ▂ ▃ ▃ ▄ ▆ ▇ █ █ █ █ █ █ █ █ ┴ 1.89512
外部链接
| Weisstein, Eric W. “指数积分”来自 MathWorld--A Wolfram Web Resource。 |