C 语言 fabs()

<style type="text/css"><!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}--> </style> c 语言 fabs 函数, c 语言中的 fabs(), 带示例的 c 语言 fabs() 函数, fabs 方法, c 语言中的 fabs 方法


double fabs(double x); #where x should be in double

 

fabs() 参数

fabs() 函数接受一个 double 类型的参数。如果要查找整数或浮点数的绝对值,我们可以将该数字显式转换为 double。

参数 描述 必需/可选
双精度浮点数值 需要查找其绝对值 必需

fabs() 返回值

fabs() 函数的返回值为 double 类型的数字。

输入 返回值
双精度浮点数值 绝对值(double 类型)

fabs() 示例

示例 1:C 语言中 fabs() 函数的工作原理?


#include <stdio.h>
#include <math.h>
int main()
{
    double k, output;

    k = -3.70;
    output = fabs(k);
    printf("The |%.2lf| =  %.2lf\n", k, output);

    k = 9.20;
    output = fabs(k);
    printf("The |%.2lf| =  %.2lf\n", k, output);

    k = 0.10;
    output = fabs(k);
    printf("The |%.2lf| =  %.2lf\n", k, output);

    return 0;

}

输出


The |-3.70| =  3.70
The |9.20| =  9.20
The |0.10| =  0.10
 

示例 2:C 语言中 fabs() 函数是如何工作的?


#include <stdio.h>
#include <math.h>
int main () {
   int x, y;
   x = 4325;
   y = -122;
  
   printf("Absolute value of %d is %lf\n", x, fabs(x));
   printf("Absolute value of %d is %lf\n", y, fabs(y));
   
   return(0);
}

输出


Absolute value of 4325 is 4325.000000
Absolute value of -122 is 122.000000