C fabs 函数

C fabs 函数是一个数学函数,可用于返回给定值或指定表达式的绝对正数。fabs 的语法如下所示。

double fabs(double number);

fabs 函数示例

数学函数允许您返回绝对正数。在此数学 fabs 程序中,我们将查找不同值的绝对正数并显示输出。

#include <stdio.h>
#include <math.h>

int main()
{
    printf("\n The Absolute Value of 0       = %.4f ", fabs(0));
    printf("\n The Absolute Value of 1       = %.4f ", fabs(1));
    
    printf("\n The Absolute Value of 152     = %.4f ", fabs(152));
    printf("\n The Absolute Value of 400.36  = %.4f ", fabs(400.36));
    
    printf("\n The Absolute Value of -27.32  = %.4f ", fabs(-27.32));  
    printf("\n The Absolute Value of -90     = %.4f ", fabs(-90));
  
    return 0;
}
C fabs Function Example

fabs 示例 2

在此 C 编程 示例中,我们允许用户输入自己的值。接下来,此程序使用 math fabs 函数来查找用户给定值的绝对正数。

#include <stdio.h>
#include <math.h>

int main()
{
float number, absValue;

printf(" Please Enter any Numeric Value : ");
scanf("%f", &number);

absValue = fabs(number);

printf("\n The Result of %.2f = %.4f ", number, absValue);

return 0;
}
 Please Enter any Numeric Value :  -23.456

 The Result of -23.46 = 23.4560