C ++中的remquo()函數(shù)計算分子/分母的浮點余數(shù),并且還將商存儲到傳遞給它的指針。
C ++中的remquo()函數(shù)計算分子/分母的浮點余數(shù)(四舍五入到最接近的值)。它還將商存儲到傳遞給它的指針。它返回與restder()函數(shù)相同的值。
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); // 用于其他算術(shù)類型組合的附加重載。
remquo()函數(shù)接受三個參數(shù),并返回double,float或long double類型的值。此功能在<cmath>頭文件中定義。
x:分子的值。
y:分母的值。
q:指向?qū)ο蟮闹羔?,?nèi)部用于確定余數(shù)的商存儲為int類型的值。
remquo()函數(shù)返回x / y的浮點余數(shù)(四舍五入到最接近的值)。如果分母y為零,則remquo()返回NaN(非數(shù)字)。
#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 << "余數(shù) " << x << "/" << y << " = " << result << endl; cout << "商 " << x << "/" << y << " = " << q << endl << endl; x = -12.5; result = remquo(x, y, &q); cout << "余數(shù) " << x << "/" << y << " = " << result << endl; cout << "商 " << x << "/" << y << " = " << q << endl << endl; y = 0; result = remquo(x, y, &q); cout << "余數(shù) " << x << "/" << y << " = " << result << endl; cout << "商 " << x << "/" << y << " = " << q << endl << endl; return 0; }
運行該程序時,輸出為:
余數(shù) 12.5/2.2 = -0.7 商 12.5/2.2 = 6 余數(shù) -12.5/2.2 = 0.7 商 -12.5/2.2 = -6 余數(shù) -12.5/0 = -nan 商 -12.5/0 = 0
#include <iostream> #include <cmath> using namespace std; int main() { int q; double x = 12.5 int y = 10; result = remquo(x, y, &q); cout << "余數(shù) " << x << "/" << y << " = " << result << endl; return 0; }
運行該程序時,輸出為:
余數(shù) 12.5/10 = 2.5 商 12.5/10 = 1