📅  最后修改于: 2020-09-25 07:56:46             🧑  作者: Mango
如前所述,modf()将数字分解为整数和小数部分。小数部分由函数返回,整数部分存储在指针传递给modf()的指针所指向的地址中作为参数。
此函数在
double modf (double x, double* intpart);
float modf (float x, float* intpart);
long double modf (long double x, long double* intpart);
double modf (T x, double* intpart); // T is an integral type
modf()具有两个参数:
modf() 函数返回传递给它的参数的小数部分。
#include
#include
using namespace std;
int main ()
{
double x = 14.86, intPart, fractPart;
fractPart = modf(x, &intPart);
cout << x << " = " << intPart << " + " << fractPart << endl;
x = -31.201;
fractPart = modf(x, &intPart);
cout << x << " = " << intPart << " + " << fractPart << endl;
return 0;
}
运行该程序时,输出为:
14.86 = 14 + 0.86
-31.201 = -31 + -0.201
#include
#include
using namespace std;
int main ()
{
int x = 5;
double intpart, fractpart;
fractpart = modf(x, &intpart);
cout << x << " = " << intpart << " + " << fractpart << endl;
return 0;
}
运行该程序时,输出为:
5 = 5 + 0