当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ remquo()用法及代码示例


此函数用于返回其参数中提到的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);

    参数:

    • ab是分子和分母的值。 q是将在其中存储商的指针。

    返回:

    • 它返回四舍五入到最接近的分子/分母的浮点余数,并将商存储在q中。

    错误:

  1. 必须提供所有三个参数,否则将给调用“ remquo”的匹配函数带来错误。
  2. 必须将第三个参数传递给指针,因为它存储了商的地址,否则将给出错误“ 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 <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; 
}

输出:

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 <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; 
}

输出:

Remainder of 12.5/2 is 0.5
Quotient of 12.5/2 is 6


相关用法


注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 remquo() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。