TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Math modf() Function

C++ Math modf() Function with tutorial for beginners and professionals with examples on isgreaterequal(), less(), islessequal(), islessgreater(), isunordered(), exp(), frexp(), ldexp(), log(), log10(), modf(), exp2(), expm1(), log1p(), log2(), logb(), scalbn(), scalbln() etc.

<< Back to CPP

C++ Math modf()

This function is used to divide a number into integral and fractional part.

For example :

2.16 = 2 + 16

Syntax

Suppose a number is 'x' and 'ptr' is the pointer to an integral part.

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);

Parameter

x: The value which is to be broken into two parts i.e(fractional and integral part).

ptr: It is the pointer to an object where the integral part of x is stored.

Return value

It returns the integral part of x.

Example 1

Let's see a simple example

#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;
}

Output:

Value of x is : 18.26
integral part of x is :18
fractional part of x is :0.26

In this example, modf() function breaks a number into fractional and integral part. Fractional part is 0.26 and integral part is 18.

Example 2

Let's see a simple example when the value of x is negative.

#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;
} 

Output:

Value of x is : -78.34
integral part of x is :-78
fractional part of x is :-0.339996

Next TopicC++ Math Functions




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf