c语言中取绝对值是哪个函数

Python020

c语言中取绝对值是哪个函数,第1张

1. C语言的库函数中提供了求绝对值的函数,函数名为 abs

2. 函数的头文件:#include

3. 函数原型:int abs (int j)

4. 函数说明:abs()用来计算参数j 的绝对值,然后将结果返回。

5. 返回值:返回参数j 的绝对值结果。

c语言中取绝对值的函数

*   ABS.C:   This   program   computes   and   displays

*   the   absolute   values   of   several   numbers.

#include     <stdio.h>

#include     <math.h>

#include     <stdlib.h>

void   main(   void   )

{int         ix   =   -4,   iy

long       lx   =   -41567L,   ly

double   dx   =   -3.141593,   dy

iy   =   abs(   ix   )

printf(   "The   absolute   value   of   %d   is   %d/n",   ix,   iy)

ly   =   labs(   lx   )

printf(   "The   absolute   value   of   %ld   is   %ld/n",   lx,   ly)

dy   =   fabs(   dx   )

printf(   "The   absolute   value   of   %f   is   %f/n",   dx,   dy   )

Output

The   absolute   value   of   -4   is   4

The   absolute   value   of   -41567   is   41567

The   absolute   value   of   -3.141593   is   3.141593

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)是不解析的,所以他的取值只能是正数或者负数。