为什么我在0.0乘以-1时得到-0.0 ??? 我已0.0浮法在java中乘以-1的浮点值
sign=-1;
float output=0.0;
JOptionPane.showmessagedialog(null,0.0*sign);
在-0.0所示的代替0
为什么我在0.0乘以-1时得到-0.0 ??? 我已0.0浮法在java中乘以-1的浮点值
sign=-1;
float output=0.0;
JOptionPane.showmessagedialog(null,0.0*sign);
在-0.0所示的代替0
此输出是从http://www.javawebtips.com/154041/
"-0.0" Is produced when a floating-point operation results in a negative floating-point number so close to 0 that cannot be represented normally.
-2.0/Float.POSITIVE_INFINITY -> -0.0
"-0.0" Is numerically identical to "0.0". However, some operations involving "-0.0" are different than the same operation with "0.0".
(-0.0) == 0.0 -> true
2.0/(0.0) - Infinity
2.0/(-0.0) ->-Infinity
谢谢您的解释 – Devu
浮点值采取具有正的和负零的答案。它看起来很古怪,但有一个很好的理由(给定浮点数的表示限制)。
如果您真的想要的结果是0
,您可以考虑将结果转换为int
。
KKK ..非常感谢您的Mr.Gain – Devu
Java浮点中的零不仅仅表示实数零。它们用于表示每个绝对幅度太小而不能表示为非零数字,每个下溢结果的数字。类似地,无穷大不仅表示除零的结果,而且表示绝对幅度太大而不能表示为有限数字的每个结果,每个溢出结果。
使零签名允许区分正下溢结果和负下溢结果,包括正如另一个答案中所述,通过下溢结果得到非零数字的正确无穷大。
这对我来说似乎是正确的行为。你期望发生什么? – Gian
我们应该得到0.0或0吗?为什么它以-0.0来到? – Devu
没有。您正在体验浮点运算的已知正确行为。 – Gian