C Library Function - fmod()
Description
C Library Function double fmod(double x, double y) Return x divided by y the remainder of
fmod() is a function in the C standard library is a function in that computes the remainder of dividing two floating-point numbers. This function returns the remainder of the division, whose sign is the same as that of the dividend.
Declaration
Below is the declaration of the fmod() function.
double fmod(double x, double y)
Parameters
x: the dividend, a floating-point number.y: the divisor, a floating-point number.
Return Value
- Return
xdivided byythe remainder, whose sign is the same asx. - If both the
yis 0, the result is undefined and a calculation error may occur.
Example
The following example demonstrates the use of the fmod() function.
Example
#include
#include
int
main
(
)
{
float
a
,
b
;
int
c
;
a
=
9.2
;
b
=
3.7
;
c
=
2
;
printf
(
"%f / %d remainder is %lf\n"
,
a
,
c
,
fmod
(
a
,
c
)
)
;
printf
(
"%f / %f remainder is %lf\n"
,
a
,
b
,
fmod
(
a
,
b
)
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
9.200000 / 2 的余数是 1.200000
9.200000 / 3.700000 的余数是 1.800000
Handling remainder calculations for multiple values
The following example shows how to handle remainder calculations for multiple values:
Example
#include
#include
int
main
(
)
{
double
numerators
[
]
=
{
5.3
,
7.1
,
4.2
,
10.0
}
;
double
denominators
[
]
=
{
2.0
,
3.0
,
2.5
,
3.5
}
;
int
num_values
=
sizeof
(
numerators
)
/
sizeof
(
numerators
[
0
]
)
;
for
(
int
i
=
0
;
i
<
num_values
;
i
++
)
{
double
x
=
numerators
[
i
]
;
double
y
=
denominators
[
i
]
;
double
result
=
fmod
(
x
,
y
)
;
printf
(
"fmod(%f, %f) = %f\n"
,
x
,
y
,
result
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
fmod(5.300000, 2.000000) = 1.300000
fmod(7.100000, 3.000000) = 1.100000
fmod(-4.200000, 2.500000) = -1.700000
fmod(10.000000, -3.500000) = 3.000000
Code Analysis
- Define two arrays containing multiple floating-point numbers
numeratorsanddenominators, representing the dividends and divisors, respectively. - Use
forLoop over each pair of dividend and divisor, calling thefmod(x, y)function to perform the remainder calculation. - Print the remainder result for each pair of values.
Use Cases
fmod() The function has a wide range of uses in many applications, including but not limited to:
- Compute the remainder of floating-point numbers, used in simulating periodic phenomena.
- Implement cyclic operations, such as periodic calculations of angles.
- Handle coordinate transformations and boundary conditions in graphics and game programming.
Summary
fmod() The function is used to compute the remainder of dividing two floating-point numbers, and is an important tool for handling floating-point remainder calculations. By using fmod(), accurate handling of floating-point remainders can be achieved in scientific computing, engineering applications, and graphics programming.
C Standard Library -