C++ Math fmod()
C++ Math fmod()
该函数查找分子/分母的浮点余数并四舍五入。
公式为fmod:
fmod= numerator-t*denominator
其中't'是分子/分母的截断值。
语法
请考虑一个分子'n'和分母'd'。语法为:
double fmod(double n,double d);
参数
n : 分子的值。
d : 分子的值分母
返回值
它返回n/d的浮点余数。
注意: 如果分母的值为零,则fmod()函数将返回NAN(非数字)。
示例1
让我们看一个具有相同类型参数的简单示例。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double n=4.2;
double d=7.8;
std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<< std::endl;
std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;
return 0;
}
输出:
The values of numerator and denominator are :4.2 , 7.8
fmod of these values is :4.2
示例2
让我们看一下具有不同类型参数的简单示例。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float n=7.8;
int d=9;
std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<< std::endl;
std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;
return 0;
}
输出:
The values of numerator and denominator are :7.8 , 9
fmod of these values is :7.8
示例3
让我们看一下分母值为零时的简单示例。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float n=16.7;
int d=0;
std::cout << "The values of numerator and denominator are :" <<n<<" , "<< d<< std::endl;
std::cout << "fmod of these values is :"<<fmod(n,d) <<std::endl;
return 0;
}
输出:
The values of numerator and denominator are :16.7 , 0
fmod of these values is :-nan