开发者

Double value negative after multiplication -Java

开发者 https://www.devze.com 2023-03-05 16:59 出处:网络
In my code i have following line. double temp=(c12*fileSize); Here c12 is double and fileSizeis double

In my code i have following line.

double temp=(c12*fileSize);
  • Here c12 is double and fileSizeis double
  • c12 have a value 1700 and
  • fileSize have a va开发者_JAVA百科lue 1944038

but after multiplication i am getting -990102696.

Can any one help me on it? Is that some size limits went wrong?


(int)1700 * (int)1944038 is equal to your -990102696.

Are you sure c12 and fileSize aren't integers? If they are, the multiplication occurs with integer types, integer overflow and it is being promoted to double afterwards.


Your c12 and fileSize are most likely ints (+1 Tomasz). Java multiplies the ints, which overflows, becoming negative, and then stores that negative value in your double. Cast c12 and fileSize to double before multiplying:

double c12 = 1700, fileSize = 1944038;
System.out.println(c12 * fileSize);

produces:

3.3048646E9


Hmm, I just tried that and I get "3.3048646E9". You shouldn't get any overflow wrap around on a double: if you exceed the maximum, it should turn into "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.

c12 = 1700.0 
// or 
filesize = 1944038f 

would help, but a filesize as float/double looks fishy.


    double c12 = 1700;
    double fileSize = 1944038;
    double temp=(c12*fileSize);
    System.out.println(temp);

Gives 3.3048646E9

I bet your c12 and fileSize are ints.

0

精彩评论

暂无评论...
验证码 换一张
取 消