我正在尝试遍历 JavaScript 中的所有 32 位浮点数,以直观地比较多项式评估的一些方法的准确性。为此,我实现了如下所示的代码。不幸的是,这段代码太慢了。
有什么办法可以提高性能吗?
在 C/C++ 中,等效代码在我的计算机上运行一分钟多一点,而我没有耐心看到这段代码需要多长时间。
function nextFloat(f) {
// Note that this moves away from 0.0
// It will fail at +/- infinity and result in an NaN
var bitRepr = floatToBits(f);
bitRepr++;
return bitsToFloat(bitRepr);
}
function prevFloat(f) {
// Note that this moves towards 0.0
// This will fail at 0.0 and result in an NaN
var bitRepr = floatToBits(f);
bitRepr--;
return bitsToFloat(bitRepr);
}
function floatToBits(f) {
var buf = new ArrayBuffer(4);
(new Float32Array(buf))[0] = …Run Code Online (Sandbox Code Playgroud)