frexp函数用法详解
frexp函数简介
- 头文件包含
#include <math.h>
- 函数定义
double frexp(double x , int * exp );
float frexpf(float x , int * exp );
long double frexpl(long double x , int * exp );
- 编译链接选项
-lm
frexp函数常见使用错误
- 链接错误
undefined reference to `frexp'
解决办法:添加链接选项
-lm
- 编译错误
warning: implicit declaration of function ‘frexp’ [-Wimplicit-function-declaration]
解决办法:包含头文件
#include <math.h>
frexp函数详细描述
这些函数用于将数字x拆分为一个归一化分数和一个指数,该指数存储在exp 中
frexp函数返回值
这些函数返回归一化分数。如果参数x不为零,则归一化分数为x乘以2的次方,其绝对值始终在1/2(含)到1(不含)的范围内,即[0.5,1)。
如果x为零,则归一化分数为零,零存储在exp 中
如果x是NaN,则返回NaN,*exp的值未指定。
如果x是正无穷大(负无穷大),则返回正无穷大(负无穷大),*exp的值未指定。
frexp函数错误码
不会发生错误。
frexp函数使用举例
下面的程序产生如下结果:
" ./a.out 2560"
frexp(2560, &e) = 0.625: 0.625 * 2\(ha12 = 2560
" ./a.out \-4"
frexp(\-4, &e) = \-0.5: \-0.5 * 2\(ha3 = \-4
Program source&
#include <math.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
double x, r;
int exp;
x = strtod(argv[1], NULL);
r = frexp(x, &exp);
printf("frexp(%g, &e) = %g: %g * %d\(ha%d = %g\en",
x, r, r, FLT_RADIX, exp, x);
exit(EXIT_SUCCESS);
}
评论区