Exp
定义于头文件 <cmath>
中。
描述
计算 2
的 num
次幂。
该库为所有 cv-unqualified 浮点类型提供了 std::exp2
的重载作为参数 num
的类型
声明
- C++23
- C++11
// 1)
/* floating-point-type */ exp2( /* floating-point-type */ num );
// 2)
float exp2f( float num );
// 3)
long double exp2l( long double num );
// 4)
template< class Integer >
double exp2 ( Integer num );
// 1)
float exp2 ( float num );
// 2)
double exp2 ( double num );
// 3)
long double exp2 ( long double num );
// 4)
float exp2f( float num );
// 5)
long double exp2l( long double num );
// 6)
template< class Integer >
double exp2 ( Integer num );
参数
num
- 浮点或整数值
返回值
如果没有发生错误,返回 num
的以 2 为底的指数。
如果因溢出导致范围错误,则返回 +HUGE_VAL
、+HUGE_VALF
或 +HUGE_VALL
。
如果因下溢导致范围错误,则返回正确结果(舍入后)。
错误处理
错误按 math_errhandling 中指定的方式报告。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果参数是 ±0
,返回 1
。如果参数是 -∞
,返回 +0
。如果参数是 +∞
,返回 +∞
。如果参数是 NaN,返回 NaN。
备注
不要求额外重载与额外重载完全相同。它们只需要足够确保对于整数类型的参数 num
,std::exp2(num)
的效果与 std::exp2(static_cast<double>(num))
相同。
示例
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
std::cout
<< "exp2(4) = "
<< std::exp2(4) << '\n'
<< "exp2(0.5) = "
<< std::exp2(0.5) << '\n'
<< "exp2(-4) = "
<< std::exp2(-4) << '\n';
// special values
std::cout
<< "exp2(-0) = "
<< std::exp2(-0.0) << '\n'
<< "exp2(-Inf) = "
<< std::exp2(-INFINITY) << '\n';
// error handling
errno = 0;
std::feclearexcept(FE_ALL_EXCEPT);
std::cout
<< "exp2(1024) = "
<< std::exp2(1024) << '\n';
if (errno == ERANGE)
std::cout
<< "errno == ERANGE: "
<< std::strerror(errno) << '\n';
if (std::fetestexcept(FE_OVERFLOW))
std::cout
<< "FE_OVERFLOW raised\n";
}
exp2(4) = 16
exp2(0.5) = 1.41421
exp2(-4) = 0.0625
exp2(-0) = 1
exp2(-Inf) = 0
exp2(1024) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised