错误C2296:'%':非法,在C ++中,左操作数的类型为'double' [英] error C2296: '%' : illegal, left operand has type 'double' in C++

查看:321
本文介绍了错误C2296:'%':非法,在C ++中,左操作数的类型为'double'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对双数字使用'%',但在C ++中不起作用.示例:

I have to use '%' with double numbers, but in C++ it doesn't work. Example:

double x;
temp = x%10;

我收到此错误:

error C2296: '%' : illegal, left operand has type 'double' 

如何在不将数字从双精度转换为整数的情况下解决此问题?如果我将其转换,我将失去小数部分,而且我也不想.

How can I solve this problem without converting the number from double to integer? If I converted it, I will lose the fractional part, and I don't want.

还有其他选择吗?

推荐答案

未定义为双打,但您可以使用 fmod 代替:

% is not defined for doubles, but you can use fmod instead:

计算除法的余数返回数字/分母的浮点余数(四舍五入为零):

Compute remainder of division Returns the floating-point remainder of numer/denom (rounded towards zero):

http://www.cplusplus.com/reference/cmath/fmod/<中的示例(适用于C ++):

#include <cmath>       /* fmod */
#include <iostream>

int main ()
{
  std::cout << "fmod of 5.3 / 2 is " <<  std::fmod (5.3, 2) << std::endl;
  return 0;
}

这篇关于错误C2296:'%':非法,在C ++中,左操作数的类型为'double'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆