std:: frexp, std:: frexpf, std:: frexpl
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
frexp
(
float
num,
int
*
exp
)
;
double
frexp
(
double
num,
int
*
exp
)
;
|
(C++23 前) | |
|
constexpr
/* floating-point-type */
frexp ( /* floating-point-type */ num, int * exp ) ; |
(C++23 起) | |
|
float
frexpf
(
float
num,
int
*
exp
)
;
|
(2) |
(C++11 起)
(C++23 起为 constexpr) |
|
long
double
frexpl
(
long
double
num,
int
*
exp
)
;
|
(3) |
(C++11 起)
(C++23 起为 constexpr) |
|
额外重载
(C++11 起)
|
||
|
定义于头文件
<cmath>
|
||
|
template
<
class
Integer
>
double frexp ( Integer num, int * exp ) ; |
(A) | (C++23 起为 constexpr) |
std::frexp
的重载,作为参数
num
的类型。
(C++23 起)
|
A)
为所有整数类型提供了额外的重载,这些类型被视为
double
。
|
(since C++11) |
目录 |
参数
| 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,则直接返回该值,并在 * exp 中存储 0 。
- 如果 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
可用于在不直接进行位操作的情况下操控浮点数的表示形式。
额外的重载并不需要完全按照 (A) 的形式提供。它们只需确保对于整数类型的实参 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 << "给定数值 " << f << " 或十六进制格式 " << std::hexfloat << f << std::defaultfloat << ",\n"; double f3; double f2 = std::modf(f, &f3); std::cout << "modf() 分解为 " << f3 << " + " << f2 << '\n'; int i; f2 = std::frexp(f, &i); std::cout << "frexp() 分解为 " << f2 << " * 2^" << i << '\n'; i = std::ilogb(f); std::cout << "logb()/ilogb() 分解为 " << f / std::scalbn(1.0, i) << " * " << std::numeric_limits<double>::radix << "^" << std::ilogb(f) << '\n'; }
可能的输出:
给定数值 123.45 或十六进制格式 0x1.edccccccccccdp+6, modf() 分解为 123 + 0.45 frexp() 分解为 0.964453 * 2^7 logb()/ilogb() 分解为 1.92891 * 2^6
参见
|
(C++11)
(C++11)
|
将数字乘以2的整数次幂
(函数) |
|
(C++11)
(C++11)
(C++11)
|
提取数字的指数
(函数) |
|
(C++11)
(C++11)
(C++11)
|
提取数字的指数
(函数) |
|
(C++11)
(C++11)
|
将数字分解为整数和小数部分
(函数) |
|
C documentation
for
frexp
|
|