相关疑难解决方法(0)

为什么用浮点(或双精度)数除零不会在Java中抛出java.lang.ArithmeticException:/ by

以下陈述java.lang.ArithmeticException: / by zero显而易见.

System.out.println(0/0);
Run Code Online (Sandbox Code Playgroud)

因为文字0被认为是int文字并且在整数算术中不允许除以零.

但是,以下情况不会抛出任何异常java.lang.ArithmeticException: / by zero.

int a = 0;
double b = 6.199;
System.out.println((b/a));
Run Code Online (Sandbox Code Playgroud)

它显示Infinity.

以下语句生成NaN(非数字),没有例外.

System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic.
Run Code Online (Sandbox Code Playgroud)

在这种情况下,两个操作数都被认为是双精度数.


同样,以下语句不会抛出任何异常.

double div1 = 0D/0; //or 0D/0D
double div2 = 0/0D; //or 0D/0D

System.out.printf("div1 = %s : div2 = %s%n", div1, div2);
System.out.printf("div1 == div2 : %b%n", div1 == div2);
System.out.printf("div1 …
Run Code Online (Sandbox Code Playgroud)

java exception dividebyzeroexception arithmeticexception

57
推荐指数
4
解决办法
4万
查看次数

除以1.0/0.0:输出为无穷大

double d=1.0/0.0;
Run Code Online (Sandbox Code Playgroud)

输出是 Infinity

double d=1/0;
Run Code Online (Sandbox Code Playgroud)

输出是ArithmeticException.

这两者有什么区别?这里的无限是什么意思?

java

5
推荐指数
1
解决办法
4466
查看次数

2
推荐指数
1
解决办法
7306
查看次数

当两个值都是double时,为什么没有ArithmeticException(除以零)?

看过Double.java的源代码和一些常量就好

/**
 * Constant for the Not-a-Number (NaN) value of the {@code double} type.
 */
public static final double NaN = 0.0 / 0.0;

/**
 * Constant for the positive infinity value of the {@code double} type.
 */
public static final double POSITIVE_INFINITY = 1.0 / 0.0;

/**
 * Constant for the negative infinity value of the {@code double} type.
 */
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
Run Code Online (Sandbox Code Playgroud)

但我想知道它为什么不抛出ArithmeticException(除以零)?

我试过了

 public static final int VALUE = 0/0;
Run Code Online (Sandbox Code Playgroud)

现在它正在抛出异常,但是当我说 …

java math android exception

2
推荐指数
1
解决办法
1464
查看次数

java中1/0和1.0/0.0之间的差异

我是Java编程语言的新手,下面提出了一个愚蠢的问题.

在java中执行1/0将生成运行时异常,因为该值未定义.

但是1.0/0.0就可以了,价值是无限的.

双数据类型有什么特别之处吗?可以帮助理解一个很好的解释?提前致谢.

java

0
推荐指数
1
解决办法
860
查看次数