[英]Double value negative after multiplication -Java
In my code i have following line. 在我的代码中,我有以下行。
double temp=(c12*fileSize);
but after multiplication i am getting -990102696
. 但乘法后我得到
-990102696
。
Can any one help me on it? 任何人都可以帮助我吗? Is that some size limits went wrong?
有些尺寸限制出错吗?
(int)1700 * (int)1944038
is equal to your -990102696
. (int)1700 * (int)1944038
等于你的-990102696
。
Are you sure c12
and fileSize
aren't integers? 你确定
c12
和fileSize
不是整数吗? If they are, the multiplication occurs with integer types, integer overflow and it is being promoted to double
afterwards . 如果是,则乘法发生在整数类型,整数溢出,并且之后被提升为
double
。
Your c12 and fileSize are most likely ints (+1 Tomasz). 您的c12和fileSize很可能是整数(+1 Tomasz)。 Java multiplies the ints, which overflows, becoming negative, and then stores that negative value in your double.
Java将溢出的整数与负值相乘,然后将负值存储在double中。 Cast c12 and fileSize to double before multiplying:
在乘法之前将c12和fileSize转换为double:
double c12 = 1700, fileSize = 1944038;
System.out.println(c12 * fileSize);
produces: 生产:
3.3048646E9
Hmm, I just tried that and I get "3.3048646E9". 嗯,我刚尝试过,我得到了“3.3048646E9”。 You shouldn't get any overflow wrap around on a double: if you exceed the maximum, it should turn into "Infinity".
你不应该在double上获得任何溢出回合:如果超过最大值,它应该变成“Infinity”。
I suspect the problem is in whatever you're trying to do to write this number. 我怀疑问题在于你要写的这个号码。 Or maybe you need to show us some more code.
或者您可能需要向我们展示更多代码。
Make a double on the fly: 飞行中加倍:
double temp=(c12*1.0*fileSize);
else, an c12 as int will be multiplied by another int, and the result overflows, and is later - too late - converted to double. 否则,作为int的c12将乘以另一个int,结果溢出,稍后 - 太晚 - 转换为double。
c12 = 1700.0
// or
filesize = 1944038f
would help, but a filesize as float/double looks fishy. 会有所帮助,但像float / double这样的文件大小看起来很可疑。
double c12 = 1700;
double fileSize = 1944038;
double temp=(c12*fileSize);
System.out.println(temp);
Gives 3.3048646E9 给出3.3048646E9
I bet your c12 and fileSize are ints. 我打赌你的c12和fileSize是整数。
使用BigDecimal乘法方法,这样可以在处理大型双数时防止潜在的过度问题 :
BigDecimal temp = (new BigDecimal(fileSize)).multiply(new BigDecimal(c12));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.