Modf() is a built-in predefined function in C programming language. This function is used for mathematical calculations. All these functions are present in the header file of C math. h’. Mostly the variable ‘double’ is used for all the functions that are available in this library and also returns the double value as the result.

Modf() Working

The modf() function works in such a way that it separates/break the given argument into two parts. One value would be an integer value, whereas the second one is the fractional value. While executing this function, it stores the integer part in the memory address that is pointed through the pointer, that is occurrs as a second argument of the function. In this way, the fractional part of the value is returned.

Syntax of modf()

Parameters

A: it is the value on which we have to apply the function. It is broken down into two parts.

B: It is a pointer type value that points towards the integer part of a, after breaking process.

Returning Value

The returning value in the fractional value of a.

Prototype of modf()

  • double modf (dble a, dble* intpart);
  • float modf (flt a, flt* intpart);
  • long double modf (lng dble a, lng dble* intpart);

Implementation of modf() Function

Example # 1

In the first step, like in all the C programs, we include C support libraries that are essential for the accomplishment of some features and operations. Similarly in this program, two header files are included. As we know about the input and output streaming, this is done by iostream. Secondly, any math operation you want to add to the program needs the involvement of the cmath library.

#include

#include

Then in the main program, a double variable will be initialized by a decimal value. And two double-type variables intpart, and the fractpart are also introduced here. These both will store the respective values after the separation.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-5.png" data-lazy- height="310" src="data:image/svg xml,” width=”625″>

The function is applied, whose return value will be stored in the variable fractpart.

Modf(x, &intpart)

This function will take two parameters in it. This function will break the decimal number into integral and fractional parts. Here, we will take another decimal but a negative number. This will again use the same method of modf in the same way.

While compiling the code, we need a compiler. That is G , an input file in which the code is present, an output file on which the result will be displayed. This needs a ‘-o’ keyword to save the output in the file.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-7.png" data-lazy- height="76" src="data:image/svg xml,” width=”479″>

On the execution, you will see that a positive decimal value is split into an integral part and in the fractional part. Whereas for the second value, the resultant values are the same, but both will be negative as the input value is negative.

Example # 2

In this example, instead of having a double or a decimal value, we will take an integer value. In the case of decimal values, by seeing the value, it became clear about the integral and the fractional part. Because the integral part is the one that lies before the decimal ‘.’. whereas the fractional part lies after the point. But the accurate answer is obtained by using the modf() function. So in the case of an integer, the decimal is not present. How can we apply the modf function in this situation? We will see it through an example. Both the intpart and the fractpart are taken as a double data type. The function of modf is applied in the same way as we have applied in the first example.

Fractpart = modf(x, &intpart);

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-9.png" data-lazy- height="238" src="data:image/svg xml,” width=”636″>

Save the code and then we will execute it with the compiler.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-12.png" data-lazy- height="61" src="data:image/svg xml,” width=”489″>

On execution, you will see that the integer as a whole is written in the place of an integral part, and then the plus sign is added and after that ‘0’ is written because the integer value did not have any decimal fraction and any fractional part. That’s why the function will automatically compute it as 0.

Example # 3

Now, we will ask the user to enter any number and then we will use the modf() function on that value so that the function can separate both the values from the variable. Starting from the two basic libraries as the previous examples had. On execution, a message will see seen on the terminal, so that the user enters the number.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-13.png" data-lazy- height="260" src="data:image/svg xml,” width=”432″>

The function will be applied to the values. The integral and the fractional part will be displayed accordingly, depending on the value that the user will enter. Now, we will execute the code and then we will enter different values to see the resultant value collectively.

First, we will enter a negative value, but it will not be a decimal value. When the function will be applied to the value, it will separate each integral and the fractional part. As there is no fractional digit, so the answer will be zero. But the negative sign will be still with the zero part.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-15.png" data-lazy- height="96" src="data:image/svg xml,” width=”484″>

But the function only separates the numbers irrespective of the sign we have applied with the integers. Now, we will enter ‘0’ as an input number. This time, for both the integral and the fractional values, the answer will be the same.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-17.png" data-lazy- height="91" src="data:image/svg xml,” width=”493″>

Example # 4

Till now, we have taken integer and double values as input for the program, but here we will take a string instead of any float, or integer data type. Regardless of the number that is a decimal number ‘5.06’. First, take two double-type variables that will be used in the modf function. And for the input value, take a string data type value that is enclosed in the double-quotes.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-19.png" data-lazy- height="335" src="data:image/svg xml,” width=”369″>

Now, apply the function on it to break the string into two parts. Close the program, save it and then go to the terminal for execution.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-21.png" data-lazy- height="104" src="data:image/svg xml,” width=”717″>

When you will execute the program, you will see that during compilation, an error has occurred and the program is terminated without execution. This indicates that ‘no matching function for call to modf()…..’. this means that the error lies in the function call.

From this, it is stated that the modf() function only takes double, integer, and float values as the parameter. It does not accept strings of the characters. That’s why the error has appeared. Although the value contains numbers with a decimal fraction as the data type of variable as a string, it means that the value was not an integer, but a string. The modf() checks the value and then executes it by separating the input number.

Conclusion

‘C modf()’ is the description of the working of modf() function in C source code that we have implemented on the Ubuntu text editor and the Linux terminal. Each example is implemented by following a different approach. Modf() breaks two values as a result. One is integral and the second is the fractional part. A built-in function takes only two parameters and on execution returns only a fractional part.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Author-Image-150×150.jpg624e983fdef64.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.