日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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