C ++中的fdim()函數(shù)采用兩個(gè)參數(shù),并返回第一個(gè)和第二個(gè)參數(shù)之間的正差。
double fdim(double x, double y); float fdim(float x, float y); long double fdim(long double x, long double y); Promoted fdim(Type1 x, Type2 y); // For other combinations of arithmetic types.
從C ++ 11開(kāi)始,如果傳遞給fdim()的參數(shù)為long double,則返回類型Promoted為long double。如果不是,則返回類型Promoted為double。
此函數(shù)在<cmath>頭文件中定義。
fdim()函數(shù)采用兩個(gè)浮點(diǎn)或整數(shù)類型的參數(shù):
x -fdim()的第一個(gè)參數(shù)
y -fdim()的第二個(gè)參數(shù)
fdim()函數(shù)返回:
若x>y,返回x-y
如果x≤y為 0
#include <iostream> #include <cmath> using namespace std; int main() { double x = 22.31, y = 13.17, result; result = fdim(x, y); cout << "fdim(x, y) = " << result << endl; long double xLD = -22.31, resultLD; y = 13.14; resultLD = fdim(xLD, y); cout << "fdim(xLD, y) = " << resultLD << endl; return 0; }
運(yùn)行該程序時(shí),輸出為:
fdim(x, y) = 9.14 fdim(xLD, yLD) = 0