当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ fdim()用法及代码示例


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

如果a> b否则返回0,则此函数返回a-b。其中,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 <cmath> 
#include <iostream> 
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 <cmath> 
#include <iostream> 
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


相关用法


注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 fdim() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。