Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
scala> 1.0 / Double.MinPositiveValue
res0: Double = Infinity
哦。恼人的。我希望我可以做类似的事情:
def f(x: Double) = 1.0 / (x + Double.MinPositiveValue)
...并避免Infinity
为f(0.0)
. 让我们尝试找到一个稍大的数字:
scala> val xs = Iterator.iterate(Double.MinPositiveValue)(_ + Double.MinPositiveValue)
xs: Iterator[Double] = non-empty iterator
scala> xs.take(10).toList
res1: List[Double] = List(4.9E-324, 1.0E-323, 1.5E-323, 2.0E-323, 2.5E-323, 3.0E-323, 3.5E-323, 4.0E-323, 4.4E-323, 4.9E-323)
好的。好的。它在增加。怎么样:
scala> xs.map(1.0 / _).take(10).toList
res2: List[Double] = List(Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity)
嗯……也许需要一段时间。我们试试看:
scala> xs.find(x => !(1.0 / x).isInfinite)
......我还在等这个。看起来它不会很快终止。
如何找到Double
不会给出无限结果的最小除数?