不能在双精度上使用模数吗? [英] Can't use modulus on doubles?
本文介绍了不能在双精度上使用模数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个C ++程序(使用g ++编译)。我试图应用两个双精度作为操作数的模函数,但我得到以下错误:
I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error:
错误:无效的操作数类型'double'and'double'to binary'operator%'
error: invalid operands of types 'double' and 'double' to binary 'operator%'
这里是代码:
int main() {
double x = 6.3;
double y = 2;
double z = x % y;
}
推荐答案
%
运算符用于整数。您正在查找 fmod()
函数。
The %
operator is for integers. You're looking for the fmod()
function.
#include <math.h>
int main() {
double x = 6.3;
double y = 2.0;
double z = fmod(x,y);
return 0;
}
这篇关于不能在双精度上使用模数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文