C 標(biāo)準(zhǔn)庫(kù) <math.h>
fabs(x)函數(shù)返回參數(shù) x 的絕對(duì)值。
double fabs (double x);
fabs()函數(shù)采用一個(gè)參數(shù)(double形式)并返回該數(shù)字的絕對(duì)值(double形式)。
[數(shù)學(xué)] |x| = fabs(x) [c 語(yǔ)言]
要查找整數(shù)或浮點(diǎn)數(shù)的絕對(duì)值,可以將數(shù)字顯式轉(zhuǎn)換為double。
int x = 0; double result; result = fabs(double(x));
fabs()函數(shù)在math.h頭文件中定義
#include <stdio.h> #include <math.h> int main() { double x, result; x = -1.5; result = fabs(x); printf("|%.2lf| = %.2lf\n", x, result); x = 11.3; result = fabs(x); printf("|%.2lf| = %.2lf\n", x, result); x = 0; result = fabs(x); printf("|%.2lf| = %.2lf\n", x, result); return 0; }
輸出結(jié)果
|-1.50| = 1.50 |11.30| = 11.30 |0.00| = 0.00