Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C modf() Function

Posted in C Tutorial
Updated on Jan 13, 2024
By Mari Selvan
👁ī¸ 185 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
C modf() Function

Photo Credit to CodeToFun

🙋 Introduction

In C programming, the modf() function is a part of the <math.h> library and is used to decompose a floating-point number into its integral and fractional parts.

This function is particularly useful when you need to separate the whole number part from the fractional part of a floating-point value.

In this tutorial, we'll explore the usage and functionality of the modf() function in C.

💡 Syntax

The syntax for the modf() function is as follows:

Syntax
Copied
Copy To Clipboard
double modf(double value, double *iptr);
  • value: The floating-point number to be decomposed.
  • iptr: A pointer to a double where the integral part of the value will be stored.

📄 Example

Let's dive into an example to illustrate how the modf() function works.

modf.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <math.h>

int main() {
  double value = 123.456;
  double integralPart;

  // Use modf() to decompose the value
  double fractionalPart = modf(value, & integralPart);

  // Output the results
  printf("Original Value: %f\n", value);
  printf("Integral Part: %f\n", integralPart);
  printf("Fractional Part: %f\n", fractionalPart);

  return 0;
}

đŸ’ģ Output

Output
Original Value: 123.456000
Integral Part: 123.000000
Fractional Part: 0.456000

🧠 How the Program Works

In this example, the modf() function is used to decompose the value 123.456 into its integral and fractional parts, which are then printed.

↩ī¸ Return Value

The modf() function returns the fractional part of the given value. The integral part is stored at the memory location pointed to by the iptr parameter.

📚 Common Use Cases

The modf() function is useful in scenarios where you need to perform calculations on the integral and fractional parts of a floating-point number separately. This is common in financial calculations, formatting numbers, or any situation where the whole and fractional parts need individual handling.

📝 Notes

  • The sign of the integral part is the same as the sign of the original value.
  • If the original value is NaN, NaN is returned, and the integral part is unspecified.
  • If the original value is ±∞, the function returns Âą0, and the integral part is ±∞.

đŸŽĸ Optimization

The modf() function is generally optimized for performance and precision. No additional optimization is typically required.

🎉 Conclusion

The modf() function in C is a valuable tool for decomposing floating-point numbers into their integral and fractional parts. It provides a convenient way to work with different aspects of numerical data, enhancing precision and flexibility in various applications.

Feel free to experiment with different values and explore the behavior of the modf() function in different scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
6 months ago

If you have any doubts regarding this article (C modf() Function) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy