frexp, frexpf, frexpl
来自cppreference.com
| 定义于头文件 <math.h>
|
||
| float frexpf( float arg, int* exp ); |
(1) | (C99 起) |
| double frexp( double arg, int* exp ); |
(2) | |
| long double frexpl( long double arg, int* exp ); |
(3) | (C99 起) |
| 定义于头文件 <tgmath.h>
|
||
| #define frexp( arg, exp ) |
(4) | (C99 起) |
1-3) 分解给定的浮点值
x 为正规化小数和二的整数幂。4) 泛型宏:若
arg 拥有 long double 类型,则调用 frexpl 。否则,若 arg 拥有整数类型或 double 类型,则调用 frexp 。否则调用 frexpf 。参数
| arg | - | 浮点值 |
| exp | - | 指向要存储指数到的整数的指针 |
返回值
若 arg 为零,则返回零并存储零于 *exp 。
否则(若 arg 非零),若不出现错误,则返回范围 (-1;-0.5], [0.5; 1) 中的值 x ,并存储整数值为 *exp ,满足 x×2(*exp)
=arg 。
若存储于 *exp 的值在 int 范围外,则行为未指定。
若 arg 不是浮点数,则行为未指定。
错误处理
此函数不受制于任何指定于 math_errhandling 的错误。
若实现支持 IEEE 浮点算术( IEC 60559 ),则
- 若
arg为 ±0 ,则返回不修改的参数,并存储0于 *exp 。 - 若
arg为 ±∞ ,则返回它,并存储未指定值于 *exp 。 - 若
arg为 NaN ,则返回 NaN ,并存储未指定值于 *exp 。 - 不引发浮点异常。
- 若 FLT_RADIX 为 2 (或 2 的幂),则返回值准确,忽略当前舍入模式。
注意
二进制系统(其中 FLT_RADIX 为 2 )上, frexp 可实现为
函数 frexp 与其对偶 ldexp 能一起用于操纵浮点数的表示,而无需直接的位操作。
示例
运行此代码
#include <stdio.h> #include <math.h> #include <float.h> int main(void) { double f = 123.45; printf("Given the number %.2f or %a in hex,\n", f, f); double f3; double f2 = modf(f, &f3); printf("modf() makes %.0f + %.2f\n", f3, f2); int i; f2 = frexp(f, &i); printf("frexp() makes %f * 2^%d\n", f2, i); i = ilogb(f); printf("logb()/ilogb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i); }
可能的输出:
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
引用
- C11 standard (ISO/IEC 9899:2011):
- 7.12.6.4 The frexp functions (p: 243)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.3.4 The frexp functions (p: 521)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.6.4 The frexp functions (p: 224)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.3.4 The frexp functions (p: 458)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.5.4.2 The frexp function
参阅
| (C99)(C99) |
将数乘以 2 的幂 (函数) |
| (C99)(C99)(C99) |
提取给定数的指数(结果为浮点数) (函数) |
| (C99)(C99)(C99) |
提取给定数的指数(结果为整数) (函数) |
| (C99)(C99) |
把一个数拆分成整数和小数部分 (函数) |