C++ 中的 fdim()


这里我们将了解 fdim() 函数在 C++ 中的用法。fdim() 函数用于返回两个给定参数之间正数差异。如果两个参数分别为 a 和 b,并且 a >b,则返回 a – b。否则返回 0。

示例

#include <cmath>
#include <iostream>
using namespace std;
main() {
   cout << "fdim of (5.0, 2.0) is " << fdim(5.0, 2.0) << endl; //positive difference
   cout << "fdim of (2.0, 5.0) is " << fdim(2.0, 5.0) << endl; //this will return 0
   cout << "fdim of (-5.0, -2.0) is " << fdim(-5.0, -2.0) << endl; //this will return 0
   cout << "fdim of (-2.0, -5.0) is " << fdim(-2.0, -5.0) << endl; //positive difference
}

输出

fdim of (5.0, 2.0) is 3
fdim of (2.0, 5.0) is 0
fdim of (-5.0, -2.0) is 0
fdim of (-2.0, -5.0) is 3

更新于: 30-Jul-2019

浏览量 161

开启你的 职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.