float之IEEE R32.24

发布于 2019-09-26 作者 风铃 20次 浏览 版块 前端

<html>
<head>
</head>
<body>
<script type="text/javascript">
/*
float 遵从的是 IEEE R32.24;
double 遵从的是 IEEE R64.53;

    float = 0x41 25 9D B2
    0b01000001 00100101 10011101 10110010

    0 - 正负 0:正,1:负
    10000010 - 指数 最高位表示正负,1:正,0-负 最高的负指数为0b01111111=+127
    010 0101 1001 1101 0111 1011 - 将小数点后的值*2,取整数部分,再将结果*2,取整数部分,循环

    1.01001011001110101111011*2的3次方 = 1010.01011001110101111011 = 10.350947
    */
    function bin2fltAfterDigit(binstr) {
        var result = 0;
        var lower = 2;
        for (var i=0; i&lt;binstr.length; i++) {
            result += parseInt(binstr[i])*(1/lower);

            lower *= 2;
        }

        return result;
    }

    console.log(&#34;bin2flt = &#34; + bin2fltAfterDigit(&#39;01011001110101111011&#39;));
    //console.log(&#34;bin2flt = &#34; + bin2fltAfterDigit(&#39;001&#39;));
&lt;/script&gt;

</body>
</html>


收藏
暂无回复