該函數用於將一個數分為整數部分和小數部分。
例如:
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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。