Namespaces
Variants

std:: scalbn, std:: scalbnf, std:: scalbnl, std:: scalbln, std:: scalblnf, std:: scalblnl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
scalbn scalbln
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
int 指数
(1)
float scalbn ( float num, int exp ) ;

double scalbn ( double num, int exp ) ;

long double scalbn ( long double num, int exp ) ;
(C++11 起)
(C++23 前)
constexpr /* 浮点类型 */
scalbn ( /* 浮点类型 */ num, int exp ) ;
(C++23 起)
float scalbnf ( float num, int exp ) ;
(2) (C++11 起)
(C++23 起 constexpr)
long double scalbnl ( long double num, int exp ) ;
(3) (C++11 起)
(C++23 起 constexpr)
long 指数
(4)
float scalbln ( float num, long exp ) ;

double scalbln ( double num, long exp ) ;

long double scalbln ( long double num, long exp ) ;
(C++11 起)
(C++23 前)
constexpr /* 浮点类型 */
scalbln ( /* 浮点类型 */ num, long exp ) ;
(C++23 起)
float scalblnf ( float num, long exp ) ;
(5) (C++11 起)
(C++23 起 constexpr)
long double scalblnl ( long double num, long exp ) ;
(6) (C++11 起)
(C++23 起 constexpr)
定义于头文件 <cmath>
template < class Integer >
double scalbn ( Integer num, int exp ) ;
(A) (C++11 起)
(C++23 起 constexpr)
template < class Integer >
double scalbln ( Integer num, long exp ) ;
(B) (C++11 起)
(C++23 起 constexpr)
1-6) 将浮点值 num 乘以 FLT_RADIX exp 次幂。 标准库为所有无 cv 限定的浮点类型提供了 std::scalbn std::scalbln 的重载版本,其中参数 num 的类型为对应浮点类型。 (C++23 起)
A,B) 为所有整数类型提供了额外的重载,这些类型将被视为 double

目录

参数

num - 浮点数或整数值
exp - 整数值

返回值

如果未发生错误,则返回 num 乘以 FLT_RADIX exp 次幂( num×FLT_RADIX exp
)。

如果发生因溢出导致的范围错误,将返回 ±HUGE_VAL ±HUGE_VALF ±HUGE_VALL

如果发生因下溢导致的范围错误,将返回正确结果(经舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

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

  • 除非发生范围错误, FE_INEXACT 永远不会被引发(结果是精确的)。
  • 除非发生范围错误, 当前舍入模式 将被忽略。
  • 如果 num 是 ±0,则直接返回该值(不作修改)。
  • 如果 num 是 ±∞,则直接返回该值(不作修改)。
  • 如果 exp 是 0,则直接返回 num (不作修改)。
  • 如果 num 是 NaN,则返回 NaN。

注释

在二进制系统(其中 FLT_RADIX 2 )中, std::scalbn 等价于 std::ldexp

尽管 std::scalbn std::scalbln 被指定为高效执行运算,但在许多实现中,它们的效率仍低于通过算术运算符进行二的幂次乘法或除法操作。

函数名代表“new scalb”,其中 scalb 是一个较旧的非标准函数,其第二个参数为浮点类型。

提供 std::scalbln 函数的原因是:从最小正浮点数值缩放到最大有限值所需的缩放因子可能超过 32767(即标准保证的 INT_MAX )。特别地,对于 80 位 long double 类型,该缩放因子为 32828。

GNU实现不会设置 errno ,无论 math_errhandling 如何配置。

额外的重载并不要求必须严格提供为 (A,B) 形式。只需确保对于整数类型的实参 num 能够满足:

  • std :: scalbn ( num, exp ) 的效果等同于 std :: scalbn ( static_cast < double > ( num ) , exp )
  • std :: scalbln ( num, exp ) 的效果等同于 std :: scalbln ( static_cast < double > ( num ) , exp )

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
    std::cout << "scalbn(7, -4) = " << std::scalbn(7, -4) << '\n'
              << "scalbn(1, -1074) = " << std::scalbn(1, -1074)
              << " (最小正次正规双精度浮点数)\n"
              << "scalbn(nextafter(1,0), 1024) = "
              << std::scalbn(std::nextafter(1,0), 1024)
              << " (最大有限双精度浮点数)\n";
    // 特殊值
    std::cout << "scalbn(-0, 10) = " << std::scalbn(-0.0, 10) << '\n'
              << "scalbn(-Inf, -1) = " << std::scalbn(-INFINITY, -1) << '\n';
    // 错误处理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "scalbn(1, 1024) = " << std::scalbn(1, 1024) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

可能的输出:

scalbn(7, -4) = 0.4375
scalbn(1, -1074) = 4.94066e-324 (最小正次正规双精度浮点数)
scalbn(nextafter(1,0), 1024) = 1.79769e+308 (最大有限双精度浮点数)
scalbn(-0, 10) = -0
scalbn(-Inf, -1) = -inf
scalbn(1, 1024) = inf
    errno == ERANGE: 数值结果超出范围
    FE_OVERFLOW raised

参见

(C++11) (C++11)
将数字分解为有效数字和以 2 为底的指数
(函数)
(C++11) (C++11)
将数字乘以 2 的整数次幂
(函数)
C 文档 关于 scalbn