该函数用于将一个数分为整数部分和小数部分。
例如:
2.16 = 2 + 16
用法
假设一个数字是 'x','ptr' 是指向整数部分的指针。
float modf(float x, float* ptr);
double modf(double x, double* ptr);
long double modf(long double x, long double* ptr);
double modf(integral x, double* ptr);
参数
x:要分成两部分的值,即(小数部分和整数部分)。
ptr:它是指向存储 x 的整数部分的对象的指针。
返回值
它返回 x 的整数部分。
例子1
让我们看一个简单的例子
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=18.26;
double ptr;
float i=modf(x,&ptr);
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"integral part of x is:"<<ptr<<'\n' ;
cout<<"fractional part of x is:"<<i;
return 0;
}
输出:
Value of x is:18.26 integral part of x is:18 fractional part of x is:0.26
在本例中,modf() 函数将一个数分解为小数部分和整数部分。小数部分为 0.26,整数部分为 18。
例子2
当 x 的值为负时,让我们看一个简单的例子。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x= -78.34;
double ptr;
float n=modf(x,&ptr);
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"integral part of x is:"<<ptr<<'\n' ;
cout<<"fractional part of x is:"<<n;
return 0;
}
输出:
Value of x is:-78.34 integral part of x is:-78 fractional part of x is:-0.339996
相关用法
- C++ Math scalbn()用法及代码示例
- C++ Math acosh()用法及代码示例
- C++ Math asinh()用法及代码示例
- C++ Math isgreater()用法及代码示例
- C++ Math fabs()用法及代码示例
- C++ Math islessgreater()用法及代码示例
- C++ Math log2()用法及代码示例
- C++ Math nearbyint()用法及代码示例
- C++ Math tan()用法及代码示例
- C++ Math log()用法及代码示例
- C++ Math nextafter()用法及代码示例
- C++ Math fdim()用法及代码示例
- C++ Math isfinite()用法及代码示例
- C++ Math erfc()用法及代码示例
- C++ Math sinh()用法及代码示例
- C++ Math scalbln()用法及代码示例
- C++ Math cosh()用法及代码示例
- C++ Math fma()用法及代码示例
- C++ Math pow()用法及代码示例
- C++ Math atan()用法及代码示例
注:本文由纯净天空筛选整理自 C++ Math modf()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。