此函数用于返回其参数中提到的2个浮点数的余数(模数),并且还存储商与传递给它的指针的商。计算出的商会四舍五入。
余数=数字– rquot *十进制
其中,rquot是以下各项的结果:分子/分母,四舍五入到最接近的整数值(中途情况四舍五入到偶数)。
句法:
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.
Parameter:
- 必须提供所有三个参数,否则将给出错误的不匹配函数来调用’remquo’ 。
- 必须将第三个参数传递给指针,因为它存储商的地址,否则将给出错误“ remquo(double&,double&,int&)”
。
错误:
例子:
Input : remquo(12.5, 2.2, &q)
Output : -0.7 and q=6
解释:
Input : remquo(-12.5, 2.2, &q)
Output : 0.7 and q=-6
#代码1
// CPP implementation of the above
// function
#include
#include
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;
}
输出 :
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
#代码2
// CPP implementation of the
// above function
#include
#include
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;
}
输出 :
Remainder of 12.5/2 is 0.5
Quotient of 12.5/2 is 6
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。