當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。