Gal*_*lon 5 java floating-point integer point division
为什么这段java代码会产生正无穷大?
double d = 10.0 / -0;
System.out.println(d);
if (d == Double.POSITIVE_INFINITY)
System.out.println("Positive Infinity");
else
System.out.println("Negative Infinity");
Run Code Online (Sandbox Code Playgroud)
Ole*_*.V. 14
虽然double区分正零和负零,int但不区分。int因此,当您将值为 0 的an 转换为 a 时double,您总是会得到 \xe2\x80\x9c 正零\xe2\x80\x9d。
-0是一个int,并且它的值为 0。
除以 \xe2\x80\x9c 负零\xe2\x80\x9d 即可获得负无穷大。为此,您需要将除数指定为 a double(而不是int):
double d = 10.0 / -0.0;\n\n System.out.println(d);\n if (d == Double.POSITIVE_INFINITY) {\n System.out.println("Positive Infinity");\n } else {\n System.out.println("Different from Positive Infinity");\n }\n if (d == Double.NEGATIVE_INFINITY) {\n System.out.println("Negative Infinity");\n } else {\n System.out.println("Different from Negative Infinity");\n }\nRun Code Online (Sandbox Code Playgroud)\n输出:
\n\n\nRun Code Online (Sandbox Code Playgroud)\n-Infinity\nDifferent from Positive Infinity\nNegative Infinity\n
| 归档时间: |
|
| 查看次数: |
3178 次 |
| 最近记录: |