日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java 1000到9999_java – 可以0.99999999999乘以四舍五入到1.0?

發布時間:2025/3/12 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。