[英]Iterating through all float values with Java
我正在開發一個數學函數,並希望在一定范圍內的每個浮點值下測試其輸出。 我已經用C ++做到了,但是現在我想將性能與Java進行比較。 如何遍歷Java中的所有float值?
在C ++中,我只是使用unsigned int遍歷了必要的范圍,然后將其重新解釋為浮點指針
float *x = reinterpret_cast<float*>(&i);
但是,如何用Java完成呢? 最好盡快,因為我正在測試性能(沒有String解決方案,謝謝:D)。 如果沒有快速的方法,我想我可以將其中的一百萬個預先計算為一個數組,然后遍歷它們。 但這會破壞高速緩存的性能,因此我認為使用隨機數將對我的情況更好,盡管它並不能完全滿足所有值。
您可以使用Math.nextUp(float)
獲取下一個浮點數。
從1開始打印下100個浮點的示例
float n = 1f;
System.out.println(n);
for (int i = 0; i < 100; i++) {
n = Math.nextUp(n);
System.out.println(n);
}
在Java中,可以使用Float.intBitsToFloat(int)
方法:
for (int i = iMin; i < iMax; i++) {
float f = Float.intBitsToFloat(i);
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.