Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

modulus
c++
fmod
communitycreator

What is the fmod() function in C++?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The fmod() function is used to return the modulus value of a fraction of two floating-point numbers (x/y) passed to the function as an argument.

In simple terms, the fmod() function returns the floating-point remainder of xy\frac{x}{y}.

Syntax

double fmod(double x, double y);
float fmod(float x, float y):
long double fmod(long double x, long double y);

Parameter value

The fmod() function takes two arguments (x and y) as its parameter values, where x and y represent the numerator and denominator, respectively.

Return value

The fmod() function returns the floating-point remainder or modulus of x and y. It returns a NaN value i.e (Not a Number) when the denominator y is given as zero.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long double x = 10.55, y = 2.65;
// using the fmod() function
long double result = fmod(x, y);
cout << "fmod(10.55, 2.65) = " << result << endl;
x = -14.88, y = 2.06;
// using the fmod() function
result = fmod(x, y);
cout << "fmod(-14.88, 2.06) =" << result << endl;
return 0;
}
Implementing the fmod() function

Code explanation

  • Line 8: We declare two doubles, x and y.

  • Line 10: We use the fmod() function to determine the remainder of x divided by y.

  • Line 11: We print the output of the fmod() function applied on x and y.

  • Line 13: We assign new values to x and y.

  • Line 15: We use the fmod() function to determine the remainder of x divided by y.

  • Line 16: We print the output of the fmod() function applied on x and y.

RELATED TAGS

modulus
c++
fmod
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring