C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Math modf()

C++ Math modf()

此函数用于将数字分为整数和小数部分。
例如:
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的整数部分的对象的指针。

返回值

它返回

示例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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4