I think its pretty self explanatory from the code. Obviously I'm no evaluating the same thing over and over, its just example numbers to explain my problem. I'm guessing its over/underflow but I don't know how to deal with it.
double d = (1 / (684985+157781));
System.out.println(d); // returns 0.0
System.out.println(Math.log(d)); // returns -Infinity.
(1 / (684985+157781))
is an integer expression, so it will come out to 0
. The zero then gets assigned to the double d
, as 0.0
.
Try changing the 1
to 1.0
to force that expression to be a float, or 1.0D
to force it to double.
另一个人通过整数除法完成:
double d = (1.0 / (684985.0+157781.0));
不,在Java中,如果使用整数,除法的结果将再次为整数,您必须将至少一个操作数转换为double。
double d = (1 / (double)(684985+157781));
Try using double d = (1.0 / (684985+157781));
Note the 1.0
part: you want to force the double evaluation.
That first expression is computed in integer arithmetic. To get the answer you're expecting, you need to compute it in floating-point arithmetic, thus:
double d = (1.0 / (684985.0+157781.0));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.