我该如何评估这个方程?
我认为从代码来看它是不言自明的。显然,我不会一遍又一遍地评估同一件事,它只是解释我的问题的示例数字。我猜测它的上溢/下溢,但我不知道如何处理它。
double d = (1 / (684985+157781));
System.out.println(d); // returns 0.0
System.out.println(Math.log(d)); // returns -Infinity.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
(1 / (684985+157781))
是一个整数表达式,因此结果将是0
。然后零被分配给
double d
,如0.0
。尝试将
1
更改为1.0
以强制该表达式为浮点数,或将1.0D
更改为双倍。(1 / (684985+157781))
is an integer expression, so it will come out to0
.The zero then gets assigned to the
double d
, as0.0
.Try changing the
1
to1.0
to force that expression to be a float, or1.0D
to force it to double.另一个人通过整数除法完成:
Another person done in by integer division:
不,在Java中如果你使用整数,除法的结果将再次是整数,你必须将至少一个操作数转换为double。
No, in Java if you use integers, the result of division would be again integer, you have to cast least one operand to double.
尝试使用 double d = (1.0 / (684985+157781));
请注意
1.0
部分:您想要强制进行双重计算。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: