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


C++ Math remainder()用法及代碼示例

該函數查找分子/分母的浮點餘數(四舍五入到最接近的整數值)。

餘數公式:

Remainder = numerator - (r*denominator)

其中,r = 分子/分母,並四舍五入到最接近的整數值。

用法

考慮分子 'n' 和分母 'd'。語法是:

return_type remainder(data_type n,data_type d);

注意:return_type 可以是 float、double 或 long double。

參數

n: 分子的值。

d:分母的值。

返回值

它返回浮點餘數 n/d。

例子1

讓我們看一個簡單的例子。

#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
    float n=5.7;
    float d=8.9;
    std::cout << "Values of numerator and denominator are:" <<n <<" , "<<d<<std::endl;
    cout<<"Remainder is:"<<remainder(n,d);
    return 0;
}

輸出:

Values of numerator and denominator are:5.7 , 8.9
Remainder is:-3.2   





相關用法


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