Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - fmod()

C Library Function - fmod()

C Standard Library - <math.h> C Standard Library -

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

Return Value

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

Use Cases

fmod() The function has a wide range of uses in many applications, including but not limited to:

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 - <math.h> C Standard Library -