📜  C++ remquo()

📅  最后修改于: 2020-09-25 08:03:00             🧑  作者: Mango

C++中的remquo() 函数计算分子/分母的浮点余数,并且还将商存储到传递给它的指针。

C++中的remquo() 函数计算分子/分母的浮点余数(四舍五入到最接近的值)。它还将商存储到传递给它的指针。它返回与剩下的函数相同的值。

remquo()原型[从C++ 11标准开始]

double remquo(double x, double y, int* q); 
float remquo(float x, float y, int* q);
long double remquo(long double x, long double y, int* q);
double remquo(Type1 x, Type2 y, int* q); // Additional overloads for other combinations of arithmetic types.

remquo() 函数采用三个参数,并返回double,float或long double类型的值。此函数在头文件中定义。

remquo()参数

remquo()返回值

remquo() 函数返回x / y的浮点余数(四舍五入到最接近的值)。如果分母y为零,则remquo()返回NaN(不是数字)。

示例1:remquo()在C++中如何工作?

#include 
#include 

using namespace std;

int main()
{
    int q;
    double x = 12.5, y = 2.2;

    double result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    cout << "Quotient of " << x << "/" << y << " = " << q << endl << endl;

    x = -12.5;
    result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    cout << "Quotient of " << x << "/" << y << " = " << q << endl << endl;

    y = 0;
    result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    cout << "Quotient of " << x << "/" << y << " = " << q << endl << endl;
    
    return 0;
}

运行该程序时,输出为:

Remainder of 12.5/2.2 = -0.7
Quotient of 12.5/2.2 = 6

Remainder of -12.5/2.2 = 0.7
Quotient of -12.5/2.2 = -6

Remainder of -12.5/0 = -nan
Quotient of -12.5/0 = 0

示例2:remquo() 函数用于不同类型的参数

#include 
#include 

using namespace std;

int main()
{
    int q;
    double x = 12.5
    int y = 10;
    
    result = remquo(x, y, &q);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    
    return 0;
}

运行该程序时,输出为:

Remainder of 12.5/10 = 2.5
Quotient of 12.5/10 = 1