C++ 中的 remainder()
下面我们来看看 C++ 的 remainder() 方法的功能。该 remainder() 函数用于计算 numrator/denominator 的浮点数余数。
所以 remainder(x, y) 如下所示。
remainder(x, y) = x – rquote * y
rquote 是 x/y 的值。它向着最近的整数舍入。此函数获取两个 double、float、long double 类型的参数,并返回与作为参数给出的类型相同的余数。第一个参数是分子,第二个参数是分母。
举例
#include <iostream>
#include <cmath>
using namespace std;
main() {
double x = 14.5, y = 4.1;
double res = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
x = -34.50;
y = 4.0;
res = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
x = 65.23;
y = 0;
res = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " is: " << res << endl;
}输出
Remainder of 14.5/4.1 is: -1.9 Remainder of -34.5/4 is: 1.5 Remainder of 65.23/0 is: nan
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP