VS2010不提供remainder这个函数,自己实现,参考官网http://www.cplusplus.com/reference/cmath/remainder/的原理。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <math.h> using namespace std; //VS2010不提供remainder这个函数,自己实现 //http://www.cplusplus.com/reference/cmath/remainder/ //Returns the floating - point remainder of numer / denom(rounded to nearest) : //remainder = numer - rquot * denom //Where rquot is the result of : numer / denom, rounded toward the nearest integral value(with halfway cases rounded toward the even number). //A similar function, fmod, returns the same but with the quotient truncated(rounded towards zero) instead. #define ROUND(d) int(d + 0.5)//四舍五入 double remainder(double numer, double denom) { int rquot = ROUND(numer / denom);//remainder和fmod的区别就是要不要四舍五入 double remainder = numer - rquot * denom; return remainder; } int main() { double da1 = fabs(fmod(5.f, 2.55f)); double da2 = fabs(remainder(5.f, 2.55f)); std::cout << "Hello World!\n" << da1 << "," << da2; }