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


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

C++ 中的remquo() 函數計算分子/分母的浮點餘數,並將商存儲到傳遞給它的指針。

C++ 中的remquo() 函數計算分子/分母的浮點餘數(四舍五入到最接近的值)。它還存儲傳遞給它的指針的商。它返回與remainder() 函數相同的值。

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 類型的值。該函數在<cmath> 頭文件中定義。

參數:

  • x:分子的值。
  • y:分母的值。
  • q:指向一個對象的指針,其中內部用於確定餘數的商存儲為類型 int.的值

返回:

remquo() 函數返回 x/y 的浮點餘數(四舍五入到最接近的值)。如果分母 y 為零,remquo() 返回 NaN(非數字)。

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

#include <iostream>
#include <cmath>

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 <iostream>
#include <cmath>

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

相關用法


注:本文由純淨天空篩選整理自 C++ remquo()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。