當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。