Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-09-22 14:17:04
forward
1684 people have browsed it

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
Copy after login

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);
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template