C++ double 型不能用%取余数


C++ double型不能实施%操作符,作为除数被除数都不可以,但可以用fmod函数,则作为除数被除数都可以,即

fmod = numer - tquot * denom //tquot 是除法结果向“下”取整(向0的方向)的结果,denom是除数

一个例子是

/* fmod example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fmod */

int main ()
{
  printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) );
  printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) );
  return 0;
}

结果是

fmod of 5.3 / 2 is 1.300000
fmod of 18.5 / 4.2 is 1.700000


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM