Home  >  Article  >  Backend Development  >  Calculate the modulus of two floating point or double numbers using C language

Calculate the modulus of two floating point or double numbers using C language

WBOY
WBOYforward
2023-09-22 14:17:041592browse

Calculate the modulus of two floating point or double numbers using C language

Here we will see how to get the modulus of two float or double type data in C. Modulo is basically finding the remainder. For this we can use the remaining() function in C. The remainder() function is used to calculate the floating point remainder of the numerator/denominator.

So, remaining(x, y) will look like below.

remainder(x, y) = x – rquote * y

rquote is the value of x/y. This will round to the nearest integer value. The function accepts two arguments of type double, float, long double and returns the remainder of the same type given as argument. The first parameter is the numerator and the second parameter is the denominator.

Example

#include <stdio.h>
#include <math.h>
main() {
   double x = 14.5, y = 4.1;
   double res = remainder(x, y);
   printf("Remainder of %lf/%lf is: %lf</p><p>",x,y, res);
   x = -34.50;
   y = 4.0;
   res = remainder(x, y);
   printf("Remainder of %lf/%lf is: %lf</p><p>",x,y, res);
   x = 65.23;
   y = 0;
   res = remainder(x, y);
   printf("Remainder of %lf/%lf is: %lf</p><p>",x,y, res);
}

Output

Remainder of 14.500000/4.100000 is: -1.900000
Remainder of -34.500000/4.000000 is: 1.500000
Remainder of 65.230000/0.000000 is: -1.#IND00

The above is the detailed content of Calculate the modulus of two floating point or double numbers using C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete