The modf()
function in C is a standard library function that breaks a floating-point number into its fractional and integral parts. It is part of the C standard library (math.h
). This function is useful for separating the fractional and integral components of a floating-point number.
Table of Contents
- Introduction
modf()
Function Syntax- Understanding
modf()
Function - Examples
- Breaking a Number into Fractional and Integral Parts
- Using
modf()
with User Input
- Real-World Use Case
- Conclusion
Introduction
The modf()
function decomposes a floating-point number into its fractional and integral parts. The integral part is stored in a separate variable, while the function returns the fractional part.
modf() Function Syntax
The syntax for the modf()
function is as follows:
#include <math.h>
double modf(double x, double *intpart);
Parameters:
x
: The floating-point number to be decomposed.intpart
: A pointer to a variable where the integral part of the number will be stored.
Returns:
- The function returns the fractional part of the number
x
.
Understanding modf() Function
The modf()
function separates a floating-point number into its fractional and integral parts. The fractional part is returned by the function, while the integral part is stored in the variable pointed to by intpart
.
Examples
Breaking a Number into Fractional and Integral Parts
To demonstrate how to use modf()
to break a number into its fractional and integral parts, we will write a simple program.
Example
#include <stdio.h>
#include <math.h>
int main() {
double value = 3.14;
double intpart;
// Break the value into fractional and integral parts
double fracpart = modf(value, &intpart);
// Print the result
printf("Value: %.2f\n", value);
printf("Integral part: %.2f\n", intpart);
printf("Fractional part: %.2f\n", fracpart);
return 0;
}
Output:
Value: 3.14
Integral part: 3.00
Fractional part: 0.14
Using modf()
with User Input
This example shows how to use modf()
to break a user-provided number into its fractional and integral parts.
Example
#include <stdio.h>
#include <math.h>
int main() {
double value, intpart;
// Get user input for the value
printf("Enter a floating-point value: ");
scanf("%lf", &value);
// Break the value into fractional and integral parts
double fracpart = modf(value, &intpart);
// Print the result
printf("Value: %.2f\n", value);
printf("Integral part: %.2f\n", intpart);
printf("Fractional part: %.2f\n", fracpart);
return 0;
}
Output (example user input "5.67"):
Enter a floating-point value: 5.67
Value: 5.67
Integral part: 5.00
Fractional part: 0.67
Real-World Use Case
Handling Monetary Values
In real-world applications, the modf()
function can be used to separate the dollar and cent parts of a monetary value.
Example: Handling Monetary Values
#include <stdio.h>
#include <math.h>
int main() {
double amount, dollars;
// Get user input for the monetary amount
printf("Enter the monetary amount: ");
scanf("%lf", &amount);
// Break the amount into dollar and cent parts
double cents = modf(amount, &dollars);
// Print the result
printf("Amount: $%.2f\n", amount);
printf("Dollars: $%.2f\n", dollars);
printf("Cents: %.2f\n", cents * 100); // Convert fractional part to cents
return 0;
}
Output (example user input "123.45"):
Enter the monetary amount: 123.45
Amount: $123.45
Dollars: $123.00
Cents: 45.00
Conclusion
The modf()
function is essential for breaking a floating-point number into its fractional and integral parts in C. It is useful in various applications, particularly in fields like finance and engineering, where such decomposition of numbers is required.