跳到主要内容

Frexp

定义于头文件 <cmath> 中。

描述

将给定的浮点值 num 分解为规范化分数和 2 的整数次幂。
该库为所有 cv-unqualified 浮点类型提供了 std::frexp 的重载,作为参数 num 的类型 (自 C++23 起).
附加重载为所有整数类型提供,它们被视为 double (自 C++11 起).

声明

// 1)
constexpr /* floating-point-type */
frexp ( /* floating-point-type */ num, int* exp );
// 2)
constexpr float frexpf( float num, int* exp );
// 3)
constexpr long double frexpl( long double num, int* exp );
附加重载
// 4)
template< class Integer >
constexpr double frexp ( Integer num, int* exp );

参数

num - 浮点或整数值 exp - 指向整数值的指针,用于存储指数

返回值

如果 num 为零,则返回零并将零存储在 *exp 中。

否则(如果 num 不为零),如果没有发生错误,则返回范围 (-1, -0.5], [0.5, 1) 内的值 x,并将整数值存储在 *exp 中,使得 x×2(*exp) == num。

如果存储在 *exp 中的值超出 int 的范围,则行为未指定。

错误处理

此函数不受 math_errhandling 中指定的任何错误影响。

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

如果 num±0,则返回该值,不进行修改,并且 0 存储在 *exp 中。
如果 num±∞,则返回该值,并将未指定的值存储在 *exp 中。
如果 num 为 NaN,则返回 NaN,并将未指定的值存储在 *exp 中。
不引发浮点异常。
如果 FLT_RADIX 为 2(或 2 的幂),则返回值为精确值,当前舍入模式将被忽略。

备注

在二进制系统(FLT_RADIX 为 2)上,std::frexp 可以实现为

{
*exp = (value == 0) ? 0 : (int)(1 + std::logb(value));
return std::scalbn(value, -(*exp));
}

函数 std::frexp 及其对偶函数 std::ldexp 可用于在不直接进行位操作的情况下操纵浮点数的表示。

不需要完全按照额外的重载提供额外的重载。
它们只需足以确保对于整数类型的参数 num
std::frexp(num, exp)std::frexp(static_cast<double>(num), exp) 具有相同的效果。

示例

#include <cmath>
#include <iostream>
#include <limits>

int main()
{
double f = 123.45;
std::cout
<< "Given the number " << f << " or "
<< std::hexfloat << f << std::defaultfloat
<< " in hex,\n";

double f3;
double f2 = std::modf(f, &f3);
std::cout
<< "modf() makes "
<< f3 << " + " << f2
< '\n';

int i;
f2 = std::frexp(f, &i);
std::cout
<< "frexp() makes "
<< f2 << " * 2^" << i
<< '\n';

i = std::ilogb(f);
std::cout
<< "logb()/ilogb() make "
<< f / std::scalbn(1.0, i)
<< " * " << std::numeric_limits<double>::radix
<< "^" << std::ilogb(f)
<< '\n';
}
可能结果
Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,
modf() makes 123 + 0.45
frexp() makes 0.964453 * 2^7
logb()/ilogb() make 1.92891 * 2^6

Frexp

定义于头文件 <cmath> 中。

描述

将给定的浮点值 num 分解为规范化分数和 2 的整数次幂。
该库为所有 cv-unqualified 浮点类型提供了 std::frexp 的重载,作为参数 num 的类型 (自 C++23 起).
附加重载为所有整数类型提供,它们被视为 double (自 C++11 起).

声明

// 1)
constexpr /* floating-point-type */
frexp ( /* floating-point-type */ num, int* exp );
// 2)
constexpr float frexpf( float num, int* exp );
// 3)
constexpr long double frexpl( long double num, int* exp );
附加重载
// 4)
template< class Integer >
constexpr double frexp ( Integer num, int* exp );

参数

num - 浮点或整数值 exp - 指向整数值的指针,用于存储指数

返回值

如果 num 为零,则返回零并将零存储在 *exp 中。

否则(如果 num 不为零),如果没有发生错误,则返回范围 (-1, -0.5], [0.5, 1) 内的值 x,并将整数值存储在 *exp 中,使得 x×2(*exp) == num。

如果存储在 *exp 中的值超出 int 的范围,则行为未指定。

错误处理

此函数不受 math_errhandling 中指定的任何错误影响。

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

如果 num±0,则返回该值,不进行修改,并且 0 存储在 *exp 中。
如果 num±∞,则返回该值,并将未指定的值存储在 *exp 中。
如果 num 为 NaN,则返回 NaN,并将未指定的值存储在 *exp 中。
不引发浮点异常。
如果 FLT_RADIX 为 2(或 2 的幂),则返回值为精确值,当前舍入模式将被忽略。

备注

在二进制系统(FLT_RADIX 为 2)上,std::frexp 可以实现为

{
*exp = (value == 0) ? 0 : (int)(1 + std::logb(value));
return std::scalbn(value, -(*exp));
}

函数 std::frexp 及其对偶函数 std::ldexp 可用于在不直接进行位操作的情况下操纵浮点数的表示。

不需要完全按照额外的重载提供额外的重载。
它们只需足以确保对于整数类型的参数 num
std::frexp(num, exp)std::frexp(static_cast<double>(num), exp) 具有相同的效果。

示例

#include <cmath>
#include <iostream>
#include <limits>

int main()
{
double f = 123.45;
std::cout
<< "Given the number " << f << " or "
<< std::hexfloat << f << std::defaultfloat
<< " in hex,\n";

double f3;
double f2 = std::modf(f, &f3);
std::cout
<< "modf() makes "
<< f3 << " + " << f2
< '\n';

int i;
f2 = std::frexp(f, &i);
std::cout
<< "frexp() makes "
<< f2 << " * 2^" << i
<< '\n';

i = std::ilogb(f);
std::cout
<< "logb()/ilogb() make "
<< f / std::scalbn(1.0, i)
<< " * " << std::numeric_limits<double>::radix
<< "^" << std::ilogb(f)
<< '\n';
}
可能结果
Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,
modf() makes 123 + 0.45
frexp() makes 0.964453 * 2^7
logb()/ilogb() make 1.92891 * 2^6