跳到主要内容

Scalbn

定义于头文件 <cmath> 中。

描述

将浮点值num乘以FLT_RADIXexp次幂。
库为所有 cv-unqualified 浮点类型提供了 std::scalbn 和 std::scalbln 的重载作为参数 num 的类型  (自 C++23 起)。​为所有整数类型提供了额外的重载,这些类型被视为 double

声明

int 指数
// 1)
constexpr /* floating-point-type */
scalbn ( /* floating-point-type */ num, int exp );
// 2)
constexpr float scalbnf( float num, int exp );
// 3)
constexpr long double scalbnl( long double num, int exp );
long 指数
// 4)
constexpr /* floating-point-type */
scalbln ( /* floating-point-type */ num, long exp );
// 5)
constexpr float scalblnf( float num, long exp );
// 6)
constexpr long double scalblnl( long double num, long exp );

附加重载
// 7)
template< class Integer >
constexpr double scalbn( Integer num, int exp );
// 8)
template< class Integer >
constexpr double scalbln( Integer num, long exp );

参数

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

返回值

如果没有错误发生,返回num乘以FLT_RADIXexp次幂(num×FLT_RADIXexp)。

如果由于溢出而发生范围错误,则返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL

如果因下溢导致范围错误,则返回正确的结果(四舍五入后)。

错误处理

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

如果实现支持 IEEE 浮点运算(IEC 60559

除非发生范围错误,否则FE_INEXACT从不抛出(结果是精确的)。除非发生范围错误,否则当前的舍入模式将被忽略。如果num±0,则返回,不修改。如果num±∞,则返回,不修改。如果exp0,则返回num,不修改。如果num是NaN,则返回NaN。

备注

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

尽管 std::scalbnstd::scalbln 被指定为高效执行操作,但在许多实现中,它们不如使用算术运算符进行二的幂的乘法或除法高效。

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

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

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

附加重载不要求完全按照附加重载提供。它们只需要足以确保对于其整数类型参数 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

Scalbn

定义于头文件 <cmath> 中。

描述

将浮点值num乘以FLT_RADIXexp次幂。
库为所有 cv-unqualified 浮点类型提供了 std::scalbn 和 std::scalbln 的重载作为参数 num 的类型  (自 C++23 起)。​为所有整数类型提供了额外的重载,这些类型被视为 double

声明

int 指数
// 1)
constexpr /* floating-point-type */
scalbn ( /* floating-point-type */ num, int exp );
// 2)
constexpr float scalbnf( float num, int exp );
// 3)
constexpr long double scalbnl( long double num, int exp );
long 指数
// 4)
constexpr /* floating-point-type */
scalbln ( /* floating-point-type */ num, long exp );
// 5)
constexpr float scalblnf( float num, long exp );
// 6)
constexpr long double scalblnl( long double num, long exp );

附加重载
// 7)
template< class Integer >
constexpr double scalbn( Integer num, int exp );
// 8)
template< class Integer >
constexpr double scalbln( Integer num, long exp );

参数

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

返回值

如果没有错误发生,返回num乘以FLT_RADIXexp次幂(num×FLT_RADIXexp)。

如果由于溢出而发生范围错误,则返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL

如果因下溢导致范围错误,则返回正确的结果(四舍五入后)。

错误处理

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

如果实现支持 IEEE 浮点运算(IEC 60559

除非发生范围错误,否则FE_INEXACT从不抛出(结果是精确的)。除非发生范围错误,否则当前的舍入模式将被忽略。如果num±0,则返回,不修改。如果num±∞,则返回,不修改。如果exp0,则返回num,不修改。如果num是NaN,则返回NaN。

备注

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

尽管 std::scalbnstd::scalbln 被指定为高效执行操作,但在许多实现中,它们不如使用算术运算符进行二的幂的乘法或除法高效。

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

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

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

附加重载不要求完全按照附加重载提供。它们只需要足以确保对于其整数类型参数 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