remainder() Function with Example in C++

C++ remainder() function: Here, we are going to learn about the remainder() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on May 25, 2020

C++ remainder() function

remainder() function is a library function of cmath header, it is used to calculate the remainder (IEC 60559), it accepts two parameters (numerator and denominator) and returns the remainder (floating-point) of numerator/denominator rounded to nearest,

    remainder = numerator - rquot * denominator

Where, rquot is the value of numerator/denominator (rounded to the nearest integral value with halfway cases rounded toward the even number.

Syntax of remainder() function:

C++11:

     double remainder (double numer     , double denom);
      float remainder (float numer      , float denom);
long double remainder (long double numer, long double denom);
     double remainder (Type1 numer      , Type2 denom);

Parameter(s):

  • numer, denom – represent the values of numerator and denominator.

Return value:

It returns the remainder.

Note:

  • If the remainder is 0, then its sign is the same as the sign of numer.
  • If the value of denom is 0, the result may either 0 or it may cause a domain error.

Example:

    Input:
    double x = 15.46;
    double y = 12.56;
    
    Function call:
    remainder(x, y);
    
    Output:
    2.9

C++ code to demonstrate the example of remainder() function

// C++ code to demonstrate the example of
// remainder() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    double x;
    double y;

    x = 10;
    y = 2;
    cout << "remainder(" << x << "," << y << "): " << remainder(x, y);
    cout << endl;

    x = 5.3;
    y = 2;
    cout << "remainder(" << x << "," << y << "): " << remainder(x, y);
    cout << endl;

    x = 15.46;
    y = 12.56;
    cout << "remainder(" << x << "," << y << "): " << remainder(x, y);
    cout << endl;

    x = -10.2;
    y = 2;
    cout << "remainder(" << x << "," << y << "): " << remainder(x, y);
    cout << endl;

    return 0;
}

Output

remainder(10,2): 0
remainder(5.3,2): -0.7
remainder(15.46,12.56): 2.9
remainder(-10.2,2): -0.2

Reference: C++ remainder() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.