Namespaces
Variants

frexp, frexpf, frexpl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
frexp
(C99) (C99)
(C99) (C23)
(C99)
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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,则直接返回该值,并在 * exp 中存储 0
  • 如果 arg 为 ±∞,则直接返回该值,并在 * exp 中存储未指定的值。
  • 如果 arg 为 NaN,则返回 NaN,并在 * exp 中存储未指定的值。
  • 不会触发浮点异常。
  • 如果 FLT_RADIX 2 (或 2 的幂次),返回值是精确的,且忽略 当前舍入模式

注释

在二进制系统(其中 FLT_RADIX 2 )中, frexp 可通过以下方式实现

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

函数 frexp 与其对应函数 ldexp 可用于在不直接进行位操作的情况下处理浮点数的表示形式。

示例

#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void)
{
    double f = 123.45;
    printf("给定数字 %.2f 或十六进制格式 %a,\n", f, f);
    double f3;
    double f2 = modf(f, &f3);
    printf("modf() 生成 %.0f + %.2f\n", f3, f2);
    int i;
    f2 = frexp(f, &i);
    printf("frexp() 生成 %f * 2^%d\n", f2, i);
    i = ilogb(f);
    printf("logb()/ilogb() 生成 %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i);
}

可能的输出:

给定数字 123.45 或十六进制格式 0x1.edccccccccccdp+6,
modf() 生成 123 + 0.45
frexp() 生成 0.964453 * 2^7
logb()/ilogb() 生成 1.92891 * 2^6

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.6.4 frexp 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.3.4 frexp 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.6.4 frexp 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.3.4 frexp 函数 (p: TBD)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.6.4 frexp 函数 (p: 243)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.3.4 frexp 函数 (p: 521)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.6.4 frexp函数(第224页)
  • 7.22 泛型数学 <tgmath.h>(第335-337页)
  • F.9.3.4 frexp函数(第458页)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.5.4.2 frexp 函数

参考

将数字乘以2的指定次幂
(函数)
(C99) (C99) (C99)
提取给定数字的指数
(函数)
(C99) (C99) (C99)
提取给定数字的指数
(函数)
(C99) (C99)
将数字分解为整数和小数部分
(函数)