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