📜  C++中的fdim()

📅  最后修改于: 2021-05-30 18:00:36             🧑  作者: Mango

fdim()函数接受两个参数,并返回第一个和第二个参数之间的正差。

如果a> b否则返回0,则此函数返回ab 。其中, a是第一个参数, b
是第二个参数。

句法:

double fdim(double a, double b);
float fdim(float a, float b);

范围:

The fdim() function takes two arguments a and b, where,  a is the first argument and b 
is the second argument.

返回:

This function returns a-b if a>b otherwise returns 0.

错误:

It is mandatory to give both the arguments otherwise it will give error
no matching function for call to 'fdim()' like this.

例子:

Inuput : fdim(2.0, 1.0)
Output : 1

Inuput : fdim(-2.0, 1.0)
Output : 0

#代码1

#include 
#include 
using namespace std;
  
int main()
{
    // positive difference for 2.0 and 1.0 is 1
    cout << "fdim of (2.0, 1.0) is " << fdim(2.0, 1.0) << endl;
  
    // here 1.0<2.0 so the output is 0
    cout << "fdim of (1.0, 2.0) is " << fdim(1.0, 2.0) << endl;
  
    // here -2.0<-1.0 so the output is 0
    cout << "fdim of (-2.0, -1.0) is " << fdim(-2.0, -1.0) << endl;
  
    // positive difference for -1.0 and -2.0 is 1
    cout << "fdim of (-1.0, -2.0) is " << fdim(-1.0, -2.0) << endl;
  
    return 0;
}

输出 :

fdim of (2.0, 1.0) is 1
fdim of (1.0, 2.0) is 0
fdim of (-2.0, -1.0) is 0
fdim of (-1.0, -2.0) is 1

#代码2

#include 
#include 
using namespace std;
  
int main()
{
    // positive difference for 5.0 and 4.0 is 1
    cout << "fdim of (5.0, 4.0) is " << fdim(5.0, 4.0) << endl;
  
    // here 4.0<5.0 so the output is 0
    cout << "fdim of (4.0, 5.0) is " << fdim(4.0, 5.0) << endl;
  
    // here -5.0<-4.0 so the output is 0
    cout << "fdim of (-5.0, -4.0) is " << fdim(-5.0, -4.0) << endl;
  
    // positive difference for 5.0 and 4.0 is 1
    cout << "fdim of (-4.0, -5.0) is " << fdim(-4.0, -5.0) << endl;
  
    return 0;
}

输出 :

fdim of (5.0, 4.0) is 1
fdim of (4.0, 5.0) is 0
fdim of (-5.0, -4.0) is 0
fdim of (-4.0, -5.0) is 1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”