remquo() in C++
This function is used to return the remainder(modulus) of 2 floating point numbers mentioned in its arguments and also stores the quotient to the pointer passed to it. The quotient computed is rounded. remainder = number – rquot * denom Where rquot is the result of: numerator/denominator, rounded toward the nearest integral value (with halfway cases rounded toward the even number). Syntax:
long double remquo(long double a, long double b, int* q); double remquo(double a, double b, int* q); float remquo(float a, float b, int* q);
a and b are the values of numerator and denominator. q is the pointer in which quotient will be stored. Return: It returns the floating point remainder of numerator/denominator rounded to nearest and stores quotient in q.
Error:
- It is mandatory to give all the three arguments otherwise it will give error no matching function for call to ‘remquo’.
- It is mandatory to pass third argument a pointer because it stores address of quotient otherwise it will give error ‘remquo(double&, double&, int&)’
Time Complexity: O(1)
Space Complexity: O(1)
Examples:
Input : remquo(12.5, 2.2, &q) Output : -0.7 and q=6
Explanation:
Input : remquo(-12.5, 2.2, &q) Output : 0.7 and q=-6
# CODE 1
CPP
// CPP implementation of the above // function #include <cmath> #include <iostream> using namespace std; int main() { int q; double a = 12.5, b = 2.2; // it will return remainder // and stores quotient in q double answer = remquo(a, b, &q); cout << "Remainder of " << a << "/" << b << " is " << answer << endl; cout << "Quotient of " << a << "/" << b << " is " << q << endl << endl; a = -12.5; // it will return remainder // and stores quotient in q answer = remquo(a, b, &q); cout << "Remainder of " << a << "/" << b << " is " << answer << endl; cout << "Quotient of " << a << "/" << b << " is " << q << endl << endl; b = 0; // it will return remainder // and stores quotient in q answer = remquo(a, b, &q); cout << "Remainder of " << a << "/" << b << " is " << answer << endl; cout << "Quotient of " << a << "/" << b << " is " << q << endl << endl; return 0; } |
OUTPUT :
Remainder of 12.5/2.2 is -0.7 Quotient of 12.5/2.2 is 6 Remainder of -12.5/2.2 is 0.7 Quotient of -12.5/2.2 is -6 Remainder of -12.5/0 is -nan Quotient of -12.5/0 is -6
# CODE 2
CPP
// CPP implementation of the // above function #include <cmath> #include <iostream> using namespace std; int main() { int q; double a = 12.5; int b = 2; // it will return remainder // and stores quotient in q double answer = remquo(a, b, &q); cout << "Remainder of " << a << "/" << b << " is " << answer << endl; cout << "Quotient of " << a << "/" << b << " is " << q << endl << endl; return 0; } |
OUTPUT :
Remainder of 12.5/2 is 0.5 Quotient of 12.5/2 is 6
Contact Us