简体   繁体   English

乘以0时NaN错误JAVA

[英]NaN error JAVA when multiplying by 0

I really can't understand why this expression gives me a NaN error only when 我真的不明白为什么这个表达式只有在以下情况下才给我NaN错误

acceleration.module - Ambient.friction = 0

:

double x = (speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x())*time;

Moreover if I add pharentesis like these: 而且,如果我添加像这样的pharentesis:

double x = ((speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x()))*time;

it gives me a NaN error again. 它再次给我一个NaN错误。

In the first, the second value is multipled by time. 在第一个中,第二个值乘以时间。

double x = (speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x())*time;

For the other, the whole thing is multiplied by time: 另一方面,整个事情乘以时间:

double x = ((speed.module * speed.direction.x()) + ((acceleration.module-Ambient.friction) * acceleration.direction.x()))*time;

This makes me think you are overflowing the double values in the second one before you multiple with time. 这使我认为您是第二个双精度值溢出,然后再乘以时间。 With double overflow you get the magic values Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY 两次溢出时,您将获得神奇的值Double.POSITIVE_INFINITY或Double.NEGATIVE_INFINITY

Most likely you are multiplting time with Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY and the result is NAN. 您很可能用Double.POSITIVE_INFINITY或Double.NEGATIVE_INFINITY乘以时间,结果是NAN。

Change the calculations to a series of steps. 将计算更改为一系列步骤。 With each step, check the result for overflow, by looking for Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY. 在每个步骤中,通过查找Double.POSITIVE_INFINITY或Double.NEGATIVE_INFINITY来检查结果是否溢出。 That will tell you where things go off the rails. 这将告诉您哪里发生了问题。 You are almost certainly overflowing somewhere, but we cannot see the numbers you are handling so I cannot tell you exactly where. 您几乎肯定会在某个地方满溢,但我们看不到您正在处理的数字,因此我无法确切告诉您。

我(受过教育的)猜测是nan已经在这个方向上,因为当模块为零时它将不确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM