C programming中math.h頭文件的fabs()函數用於獲取浮點數的絕對值。該函數返回雙精度的絕對值。
用法:
double fabs (double a);
參數:它將采用一個要轉換為絕對值的參數。
返回值:在將值傳遞給fabs()時,我們可以將數字顯式轉換為雙精度。
時間複雜度:O(1)
輔助空間:O(1)
例子:
int marks = 90;
double percent;
percent = fabs(double(marks)); // This will convert marks to double type explicitly.
示例 1:下麵是顯示fabs()函數使用的C程序。
C
// C program to show the
// use of fabs() function
#include <math.h>
#include <stdio.h>
int main()
{
double a = 980;
double b = -1231;
double res;
res = fabs(a);
printf("The absolute value of %.3lf is %.3lf\n",
a, res);
res = fabs(b);
printf("The absolute value of %.3lf is %.3lf\n",
b, res);
return 0;
}
輸出
The absolute value of 980.000 is 980.000 The absolute value of -1231.000 is 1231.000
示例 2:下麵的 C 程序顯示了當 fabs() 函數用於 int 和長雙精度數時會發生什麽。
C
// C Program to show use of fabs() function
// with int and long double numbers
#include <math.h>
#include <stdio.h>
// Driver code
int main()
{
long double a = -7.546;
long double b = 4.980;
double res;
res = fabs(a);
printf("The absolute value of %.3lf is %.3lf\n",
a, res);
res = fabs(b);
printf("The absolute value of %.3lf is %.3lf\n",
b, res);
return 0;
}
輸出
The absolute value of 7.546 is 0.000 The absolute value of 4.980 is 0.000
C語言中的stdlib.h頭文件中有類似abs() and labs()的函數。
相關用法
- C語言 fabs()用法及代碼示例
- C語言 floor()用法及代碼示例
- C語言 fmod()用法及代碼示例
- C語言 free()用法及代碼示例
- C語言 fclose()用法及代碼示例
- C語言 feof()用法及代碼示例
- C語言 ferror()用法及代碼示例
- C語言 fflush()用法及代碼示例
- C語言 fgetc()用法及代碼示例
- C語言 fgetpos()用法及代碼示例
- C語言 fgets()用法及代碼示例
- C語言 fputc()用法及代碼示例
- C語言 fputs()用法及代碼示例
- C語言 fread()用法及代碼示例
- C語言 freopen()用法及代碼示例
- C語言 frexp()用法及代碼示例
- C語言 fscanf()用法及代碼示例
- C語言 fseek()用法及代碼示例
- C語言 fsetpos()用法及代碼示例
- C語言 fwrite()用法及代碼示例
- C語言 fillellipse()用法及代碼示例
- C語言 fillpoly()用法及代碼示例
- C語言 ftell()用法及代碼示例
- C語言 fprintf()用法及代碼示例
- C語言 fork()用法及代碼示例
注:本文由純淨天空篩選整理自kamleshjoshi18大神的英文原創作品 fabs() Function in C。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。