fabs()函数的用法:double fabs(double x)。其中参数x 是浮点值,这个函数返回x的绝对值。代码示例如下:
int main (){
int a, b
a = 1234
b = -344
printf("The absolute value of %d is %lf", a, fabs(a))
printf("The absolute value of %d is %lf", b, fabs(b))
return(0)}
编译和运行上面的程序,这将产生以下结果:
The absolute value of 1234 is 1234.000000
The absolute value of -344 is 344.000000
fabs()和abs()区别:
c语言中函数abs和fabs只有一个区别:abs函数是求整数的绝对值,函数原型是int abs(int x);fabs函数是求浮点数的绝对值,函数原型是float fabs(float x)。
abs函数是一种用于求绝对值的LV函数。因为abs(x)在0点的导数是不存在的,而对于x为复数 abs(x)是不解析的,所以他的取值只能是正数或者负数。
用法:#include <math.h>
fabs函数是一个求绝对值的函数,求出x的绝对值,和数学上的概念相同,函数原型是extern float fabs(float x),用法是#include <math.h>。
程序判断滤波的C程序函数如下:
float program_detect_filter(float old_new_value[], float X)
{
float sample_value
if (fabs(old_new_value[1]_old_new_value[0])>X)
sample_value=old_new_value[0]
else
sample_value=old_new_value[1]
retrun(sample_value)
}
函数调用需一个一维的两个元素的数组(old_new_value[2],用于存放上次采样值(old_new_value[0],)和本次采样值(old_new_value[1],),函数中sample_value表示有效采样值,X表示根据根据经验确定的两次采样允许的最大偏差△×。
扩展资料功能:C语言中用来求浮点数x的绝对值
用法:#include <math.h>使用的时候头文件中加上这个就可以直接调用了
说明:计算|x|, 当x不为负时返回 x,否则返回 -x
比如:
#include <stdio.h>
#include <math.h>
int main(void)
{
float number = -1234.0
printf("number:%fabsolutevalue:%f\n", number, fabs(number))
return 0
}
这里通过fabs()函数就可以成功输出浮点数-1234.0的绝对值了
参考资料来源:百度百科-fabs函数
fabs是求浮点数的绝对值的库函数。它有一个double型形参,返回一个double型数据。当不关心浮点数的符号只关心其数字部分时用fabs取得其绝对值。举例代码如下:
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"
#include "math.h"
int main(void){
double x=3.1415926,y=-x
printf("x = %f\tfabs(x) = %f\n",x,fabs(x))
printf("y = %f\tfabs(y) = %f\n",y,fabs(y))
return 0
}
输出是: