整数の剰余には%を使う。しかし小数には使えない。どうすれば良いか。
#include<iostream> int main(void){ int a = 8; int b = 3; std::cout << "a % b = " << a % b << std::endl; long double c = 8.5L; long double d = 3.5L; // std::cout << "c % d = " << c % d << std::endl; // error return 0; }
std::fmod()を使うのが正解。
#include<iostream> #include<cmath> int main(void){ long double c = 8.5L; long double d = 3.5L; std::cout << std::fmod(c, d) << std::endl; // 1.5 return 0; }