评论者:slipset
所以,我觉得有些除以零是无定义的。
如果移除
`
if(yops.isZero((Number)y))
throw new ArithmeticException("Divide by zero");
`
那么
(/ (identity 5.0) (identity 0.0))
返回了未装箱版本的无限大。
尽管这样做,也会改变
(/ 5 0)
使其变为1/0
,而之前它会抛出除以零的错误。
从Numbers.java中我们看到非装箱数学无法防止除以零
`
static public double divide(double x, double y){
return x / y;
}
`
而装箱版本可以
`
static public Number divide(Object x, Object y){
if (isNaN(x)){
return (Number)x;
} else if(isNaN(y)){
return (Number)y;
}
Ops yops = ops(y);
if(yops.isZero((Number)y))
throw new ArithmeticException("Divide by zero");
return ops(x).combine(yops).divide((Number)x, (Number)y);
}
`
此不一致性也在Clojure 1.8中可重现
`
Clojure 1.8.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_144-b01
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
源代码: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
结果:存储在变量1, 2, 3,在e中发生异常
user=> (/ 5.0 0.0)
Infinity
user=> (/ (identity 5.0) (identity 0.0))
ArithmeticException Divide by zero clojure.lang.Numbers.divide (Numbers.java:158)
user=>
`