C语言 使用math.h的modf()函数,获取整数和小数部分
2021-0827 blog_003
#include <stdio.h>
#include <math.h>
double* math_modf();
int main(int argc, char*[])
{
printf("funciton :main()\n");
double* temp;
temp = math_modf();
printf("function return :%lf, %lf\n", *temp, *temp++);
return 0;
}
double* math_modf()
{
double temp = 3.1415926;
double int_part = 0, fraction_part = 0;
static double rtn_value[2] = { 0 };
rtn_value[0] = 0;
rtn_value[1] = 0;
fraction_part = modf(temp, &int_part);
printf("double number :%lf, scientific e :%e\n", temp, temp);
printf("int part :%lf, fraction part :%lf\n", int_part, fraction_part);
rtn_value[0] = int_part;
rtn_value[1] = fraction_part;
return rtn_value;
}
C 库函数 math.h
double modf(double x, double *integer)
返回值为小数部分(小数点后的部分),并设置 integer 为整数部分。
下面是 modf() 函数的声明。
double modf(double x, double *integer)
参数1::x – 浮点值。待解析的双精度浮点数
参数2:integer – 指向一个对象的指针,该对象存储了整数部分。
返回值
该函数返回 x 的小数部分,符号与 x 相同。