C++ Reference: Standard C++ Library reference: C Library: cmath: remainder
C++官网参考链接:https://cplusplus.com/reference/cmath/remainder/
函数
<cmath> <ctgmath>
remainder
C90
double remainder(double numer, double denom);
C99
double remainder(double numer, double denom);
float remainderf(float numer, float denom);
long double remainderl(long double numer, long double denom);
C++98
double remainder(double numer, double denom);
float remainder(float numer, float denom);
long double remainder(long double numer, long double denom);
C++11
double remainder(double numer, double denom);
float remainder(float numer, float denom);
long double remainder(long double numer, long double denom);
double remainder(Type1 numer, Type2 denom); // additional overloads
计算余数(IEC 60559)
返回numer/denom的浮点余数(舍入到最近的整数值):
remainder = number - rquot * denom
其中rquot是number/denom舍入到最近的整数值的结果(中间的情况舍入到偶数)。
类似的函数fmod返回相同的结果,但是商被截断(向零舍入)。
函数remquo的行为与此函数相同,但它还提供了对所使用的中间商值的访问。
C99
头文件<tgmath.h>提供了该函数的类型泛型宏版本。
C++11
这个头文件(<cmath>)为其他算术类型(arithmetic types)(Type1和Type2)的组合提供了额外的重载:这些重载在计算之前有效地将其实参转换为double类型,除非至少有一个实参是long double类型(在这种情况下,两个实参都被转换为long double类型)。
参数
numer
商分子的值。
denom
商分母的值。
返回值
除参数的余数。
如果这个余数为零,它的符号应为numer的符号。
如果denom为零,函数可能返回零或导致域错误(取决于库实现)。
如果发生域错误:
—math_errhandling设置了MATH_ERRNO:全局变量errno设置为EDOM。
—math_errhandling设置了MATH_ERREXCEPT:将引发FE_INVALID。
用例
/* remainder example */
#include <stdio.h> /* printf */
#include <math.h> /* remainder */
int main ()
{
printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
return 0;
}
输出:
另请参考
fmod Compute remainder of division (function) (计算除法的余数(函数))
fabs Compute absolute value (function) (计算绝对值(函数))
round Round to nearest (function) (舍入到最近(函数))