C++ 中的remainder() 函數計算分子/分母的浮點餘數(四舍五入到最接近的值)。
C++ 中的remainder() 函數計算分子/分母的浮點餘數(四舍五入到最接近的值)。
remainder (x, y) = x - rquote * y
其中 rquote
是 x/y
的結果,向最接近的整數值四舍五入(中途情況向偶數四舍五入)。
remainder() 原型 [從 C++ 11 標準開始]
double remainder(double x, double y); float remainder(float x, float y); long double remainder(long double x, long double y); double remainder(Type1 x, Type2 y); // Additional overloads for other combinations of arithmetic types
remainder() 函數接受兩個參數並返回 double、float 或 long double 類型的值。
該函數在<cmath> 頭文件中定義。
參數:
- x- 分子的值。
- y- 分母的值。
返回:
remainder() 函數返回 x/y
的浮點餘數(四舍五入到最接近的值)。
如果分母 y
為零,則 remainder() 返回 NaN
(不是數字)。
示例 1:remainder() 如何在 C++ 中工作?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 7.5, y = 2.1;
double result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
x = -17.50, y=2.0;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
y=0;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
return 0;
}
運行程序時,輸出將是:
Remainder of 7.5/2.1 = -0.9 Remainder of -17.5/2 = 0.5 Remainder of -17.5/0 = -nan
示例 2:remainder() 函數用於不同類型的參數
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x = 5;
double y = 2.13, result;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
return 0;
}
運行程序時,輸出將是:
Remainder of 5/2.13 = 0.74
相關用法
- C++ remainder()用法及代碼示例
- C++ remquo()用法及代碼示例
- C++ remove()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ rename()用法及代碼示例
- C++ rewind()用法及代碼示例
- C++ realloc()用法及代碼示例
- C++ rename用法及代碼示例
- C++ real()用法及代碼示例
- C++ rint(), rintf(), rintl()用法及代碼示例
- C++ rotate用法及代碼示例
- C++ ratio_less_equal()用法及代碼示例
- C++ rint()用法及代碼示例
- C++ ratio_less()用法及代碼示例
- C++ round()用法及代碼示例
- C++ raise()用法及代碼示例
- C++ ratio_greater()用法及代碼示例
- C++ ratio_not_equal()用法及代碼示例
- C++ ratio_greater_equal()用法及代碼示例
- C++ ratio_equal()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ remainder()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。