我最终根据Wikipedia页面上的信息实现了自己的解析器。它可能不是最快的,但是我对此不太担心。这里是那些好奇的人:
function float16_to_float(h) { var s = (h & 0x8000) >> 15; var e = (h & 0x7C00) >> 10; var f = h & 0x03FF; if(e == 0) { return (s?-1:1) * Math.pow(2,-14) * (f/Math.pow(2, 10)); } else if (e == 0x1F) { return f?NaN:((s?-1:1)*Infinity); } return (s?-1:1) * Math.pow(2, e-15) * (1+(f/Math.pow(2, 10)));}function test() { float16_to_float(parseInt('3C00', 16)); // 1 float16_to_float(parseInt('C000', 16)); // -2 float16_to_float(parseInt('7BFF', 16)); // 6.5504 × 10^4 (Maximum half precision) float16_to_float(parseInt('3555', 16)); // 0.33325... ≈ 1/3 // Works with all the test cases on the wikipedia page}


