java 1000到9999_java – 可以0.99999999999乘以四舍五入到1.0?
如果你用someInt(> 0)乘以低于1.0的最大值,結果永遠不會是someInt。
這可以窮盡地測試整數,像這樣:
Double greatestLessThanOne = Double.longBitsToDouble(4607182418800017407L);
// Assert that greatestLessThanOne is indeed the largest double less than 1.
//assert 1.0 == greatestLessThanOne + Math.ulp(greatestLessThanOne);
for (int i = 1; i >= 0; i++)
if ((int) (greatestLessThanOne * i) == i)
System.out.println("Exception found: " + i);
該代碼段不產生輸出。
(Math.ulp返回給定double和下一個大的double值之間的距離,因此斷言確保highestLessThanOne確實是小于1.0的最大值)。
換句話說,你的行
Object element = elementArray[(int)(Math.random() * elementArray.length)];
永遠不會產生ArrayIndexOutOfBoundsException。
此外,根據Mark Dickinsons的評論here,這也適用于乘以一個雙。
With IEEE 754 floating-point arithmetic in round-to-nearest mode, you can show that x * y < y for any x < 1.0 and any non-tiny positive y. (It can fail if y is either subnormal or the smallest positive normal number.)
總結
以上是生活随笔為你收集整理的java 1000到9999_java – 可以0.99999999999乘以四舍五入到1.0?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言调用android surface
- 下一篇: harmonyos sdk,Harmon