📜  C++中的restder()

📅  最后修改于: 2021-05-30 02:39:58             🧑  作者: Mango

该函数还用于返回其参数中提到的2个浮点数的余数(模)。

余数=数字– rquot *十进制

其中,rquot是以下值的结果:numer / denom,四舍五入到最接近的整数值(中间的情况下四舍五入到偶数)。

句法 :

double remainder(double a, double b)
float remainder(float a, float b)
long double remainder(long double a, long double b)
Parameter:
a and b are the values 
of numerator and denominator.


Return:
The remainder() function returns the floating 
point remainder of numerator/denominator 
rounded to nearest.

错误或异常:必须同时提供两个参数,否则将产生错误–此类调用’remainder()’的函数不匹配

#代码1

// CPP program to demonstrate 
// remainder() function
#include 
#include 
  
using namespace std;
  
int main()
{
    double a, b;
    double answer;
  
    a = 50.35;
    b = -4.1;
  
    // here quotient is -12.2805 and rounded to nearest value then
    // rquot = -12.
    // remainder = 50.35 – (-12 * -4.1)
    answer = remainder(a, b);
  
    cout << "Remainder of " << a << "/" << b << " is " << answer << endl;
  
    a = 16.80;
    b = 3.5;
  
    // here quotient is 4.8 and rounded to nearest value then
    // rquot = -5.
    // remainder = 16.80 – (5 * 3.5)
    answer = remainder(a, b);
  
    cout << "Remainder of " << a << "/" << b << " is " << answer << endl;
  
    a = 16.80;
    b = 0;
    answer = remainder(a, b);
    cout << "Remainder of " << a << "/" << b << " is " << answer << endl;
  
    return 0;
}

输出 :

Remainder of 50.35/-4.1 is 1.15
Remainder of 16.8/3.5 is -0.7
Remainder of 16.8/0 is -nan

#代码2

// CPP program to demonstrate 
// remainder() function
#include 
#include 
  
using namespace std;
  
int main()
{
    int a = 50;
    double b = 41.35, answer;
  
    answer = remainder(a, b);
    cout << "Remainder of " << a << "/" << b << " = " << answer << endl;
  
    return 0;
}

输出 :

Remainder of 50/41.35 = 8.65
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”