C++ 中的 fabs()


C 或 C++ 庫函式 double fabs(double x) 返回 x 的絕對值。x − 這是浮點值。此函式返回 x 的絕對值。以下是 fabs() 函式的宣告。

double fabs(double x)

下面的示例顯示了 fabs() 函式的用法。

示例

 線上演示

#include <iostream>
#include <cmath>
using namespace std;
int main () {
   int a, b;
   a = 1234;
   b = -344;
   cout << "The absolute value of " << a << " is " << fabs(a) <<
   endl;
      cout << "The absolute value of " << b << " is " << fabs(b) <<
   endl;
      return(0);
}

輸出

The absolute value of 1234 is 1234
The absolute value of -344 is 344

更新時間: 2019 年 7 月 30 日

514 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.