C ++中的fdim()函数采用两个参数,并返回第一个和第二个参数之间的正差。
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开始,如果传递给fdim()的参数为long double,则返回类型Promoted为long double。如果不是,则返回类型Promoted为double。
此函数在<cmath>头文件中定义。
fdim()函数采用两个浮点或整数类型的参数:
x -fdim()的第一个参数
y -fdim()的第二个参数
fdim()函数返回:
若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;
}
运行该程序时,输出为:
fdim(x, y) = 9.14
fdim(xLD, yLD) = 0