std::scalbn, std::scalbnf, std::scalbnl, std::scalbln, std::scalblnf, std::scalblnl
来自 cppreference.cn
定义于头文件 <cmath> |
||
int exponent |
||
(1) | ||
float scalbn ( float num, int exp ); double scalbn ( double num, int exp ); |
(since C++11) (until C++23) |
|
constexpr /* floating-point-type */ scalbn ( /* floating-point-type */ num, int exp ); |
(since C++23) | |
float scalbnf( float num, int exp ); |
(2) | (since C++11) (constexpr since C++23) |
long double scalbnl( long double num, int exp ); |
(3) | (since C++11) (constexpr since C++23) |
long exponent |
||
(4) | ||
float scalbln ( float num, long exp ); double scalbln ( double num, long exp ); |
(since C++11) (until C++23) |
|
constexpr /* floating-point-type */ scalbln ( /* floating-point-type */ num, long exp ); |
(since C++23) | |
float scalblnf( float num, long exp ); |
(5) | (since C++11) (constexpr since C++23) |
long double scalblnl( long double num, long exp ); |
(6) | (since C++11) (constexpr since C++23) |
定义于头文件 <cmath> |
||
template< class Integer > double scalbn( Integer num, int exp ); |
(A) | (since C++11) (constexpr since C++23) |
template< class Integer > double scalbln( Integer num, long exp ); |
(B) | (since C++11) (constexpr since C++23) |
1-6) 将浮点数值 num 乘以 FLT_RADIX 的 exp 次幂。 库为所有 cv 限定符不修饰的浮点类型提供了
std::scalbn
和 std::scalbln
的重载,作为参数 num 的类型。(since C++23)A,B) 为所有整数类型提供了额外的重载,这些整数类型被视为 double 类型。
内容 |
[编辑] 参数
num | - | 浮点数或整数值 |
exp | - | 整数值 |
[编辑] 返回值
如果没有错误发生,则返回 num 乘以 FLT_RADIX 的 exp 次幂 (num×FLT_RADIXexp
)。
如果发生溢出范围错误,则返回 ±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
被指定为高效执行操作,但在许多实现中,它们的效率不如使用算术运算符乘以或除以 2 的幂。
函数名代表 "new scalb",其中 scalb
是一个较旧的非标准函数,其第二个参数具有浮点类型。
提供 std::scalbln
函数是因为从最小正浮点值缩放到最大有限值所需的因子可能大于 32767,即标准保证的 INT_MAX。 特别是对于 80 位 long double,因子是 32828。
无论 math_errhandling
如何,GNU 实现都不会设置 errno
。
额外的重载不需要完全按照 (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) << " (minimum positive subnormal double)\n" << "scalbn(nextafter(1,0), 1024) = " << std::scalbn(std::nextafter(1,0), 1024) << " (largest finite double)\n"; // special values std::cout << "scalbn(-0, 10) = " << std::scalbn(-0.0, 10) << '\n' << "scalbn(-Inf, -1) = " << std::scalbn(-INFINITY, -1) << '\n'; // error handling 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 (minimum positive subnormal double) scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double) scalbn(-0, 10) = -0 scalbn(-Inf, -1) = -inf scalbn(1, 1024) = inf errno == ERANGE: Numerical result out of range FE_OVERFLOW raised
[编辑] 参见
(C++11)(C++11) |
将数字分解为尾数和以 2 为底的指数 (函数) |
(C++11)(C++11) |
将数字乘以 2 的整数次幂 (函数) |
C 文档 for scalbn
|