命名空间
变体
操作

std::exp2, std::exp2f, std::exp2l

来自 cppreference.cn
< cpp‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
(C++11 起)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11 起)(C++11 起)(C++11 起)
指数函数
exp2
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
(C++11)
(C++11)
三角
双曲函数
(C++11)
(C++11)
(C++11)

误差函数和伽玛函数
(C++11)
(C++11)
(C++11)
(C++11)
最近整数浮点运算
(C++11 起)(C++11 起)(C++11 起)
(C++11)
(C++11)
(C++11 起)(C++11 起)(C++11 起)
浮点数操作函数
(C++11 起)(C++11 起)
(C++11)
(C++11)
(C++11 起)(C++11 起)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
(C++11 起)(C++11 起)
(C++11)
(C++11)
分类
(C++11 起)(C++11 起)(C++11 起)(C++11 起)(C++11 起)


 
定义于头文件 <cmath>
(1)
float       exp2 ( float num );

double      exp2 ( double num );

long double exp2 ( long double num );
(直到 C++23)
/*浮点类型*/
            exp2 ( /*浮点类型*/ num );
(C++23 起)
(constexpr 自 C++26 起)
float       exp2f( float num );
(2) (C++11 起)
(constexpr 自 C++26 起)
long double exp2l( long double num );
(3) (C++11 起)
(constexpr 自 C++26 起)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template< /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/<V>

            exp2 ( const V& v_num );
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template< class Integer >
double      exp2 ( Integer num );
(A) (constexpr 自 C++26 起)
1-3) 计算给定幂 num2 次方。 库为所有 cv 限定非限定浮点类型提供了 std::exp2 的重载作为形参类型。(C++23 起)
S) SIMD 重载对 v_num 执行逐元素的 std::exp2
(参见 math-floating-pointdeduced-simd-t 以了解其定义。)
(C++26 起)
A) 为所有整数类型提供了附加重载,它们被视为 double
(C++11 起)

内容

[编辑] 参数

num - 浮点值或整数值

[编辑] 返回值

若无错误发生,则返回 num 的以 2 为底的指数值 (2num
)。

若因上溢发生值域错误,则返回 +HUGE_VAL+HUGE_VALF+HUGE_VALL

若因下溢发生值域错误,则返回正确结果(舍入后)。

[编辑] 错误处理

错误按 math_errhandling 中指定的方式汇报。

若实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为 ±0,则返回 1。
  • 若参数为 -∞,则返回 +0。
  • 若参数为 +∞,则返回 +∞。
  • 若参数为 NaN,则返回 NaN。

[编辑] 注解

附加重载不需要精确地如同 (A) 提供。它们仅需足以保证对于整数类型的实参 numstd::exp2(num) 具有与 std::exp2(static_cast<double>(num)) 相同的效果。

对于整数指数,可能优先使用 std::ldexp

[编辑] 示例

#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);
    const double inf = std::exp2(1024);
    const bool is_range_error = errno == ERANGE;
 
    std::cout << "exp2(1024) = " << inf << '\n';
    if (is_range_error)
        std::cout << "    errno == ERANGE: " << std::strerror(ERANGE) << '\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

[编辑] 参见

(C++11 起)(C++11 起)
返回 e 的给定次幂 (ex)
(函数) [编辑]
(C++11 起)(C++11 起)(C++11 起)
返回 e 的给定次幂,减 1 (ex-1)
(函数) [编辑]
(C++11 起)(C++11 起)
将数字乘以 2 的整数次幂
(函数) [编辑]
(C++11 起)(C++11 起)(C++11 起)
给定数字的以 2 为底的对数 (log2(x))
(函数) [编辑]
C 文档 关于 exp2