如何从Javascript中的Sign,Mantissa和Exponent获取十进制

时间:2018-01-04 23:08:52

标签: javascript ieee-754

我试图获得小数/浮点数的ieee754 32位表示。我使用此代码来获取尾数,符号和指数:

function decodeIEEE64 ( value ) {

  if ( typeof value !== "number" )
    throw new TypeError( "value must be a Number" );

  var result = {
    isNegative : false,
    exponent : 0,
    mantissa : 0
  };

  if ( value === 0 ) {
    return result;
  }

  // not finite?
  if ( !isFinite( value ) ) {
    result.exponent = 2047;
    if ( isNaN( value ) ) {
      result.isNegative = false;
      result.mantissa = 2251799813685248; // QNan
    } else {
      result.isNegative = value === -Infinity;
      result.mantissa = 0;
    }
    return result;
  }

  // negative?
  if ( value < 0 ) {  result.isNegative = true;  value = -value;  }

  // calculate biased exponent
  var e = 0;
  if ( value >= Math.pow( 2, -1022 ) ) { // not denormalized
    // calculate integer part of binary logarithm
    // http://en.wikipedia.org/wiki/Binary_logarithm
    var r = value;
    while ( r < 1 ) { e -= 1; r *= 2; }
    while ( r >= 2 ) { e += 1; r /= 2; }
    e += 1023; // add bias
  }
  result.exponent = e;

  // calculate mantissa
  if ( e != 0 ) {
    var f = value / Math.pow( 2, e - 1023 );
    result.mantissa = Math.floor( (f - 1) * Math.pow( 2, 52 ) );
  } else { // denormalized
    result.mantissa = Math.floor( value / Math.pow( 2, -1074 ) );
  }

  return result;
}

var results = decodeIEEE64(0.07);
console.log(results);

我尝试使用的示例是0.07。我应该能够将32位ieee754作为0.070000000298 ...

对于0.07,我的代码会给我{isNegative: false, exponent: 1019, mantissa: 540431955284460}我相信我应该将其格式化为sign 2^exponent mantissa

如果我sign 2^exponent mantissa我得到:0-5.61779105e306-540431955284460这让我相信我应该首先转换为二进制。为此,我尝试使用此代码:

function toBinary (decimal) {
    return decimal.toString(2);
}

所以我toBinary(Math.pow(2, exponent))toBinary(mantissa)现在得到0-100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000-1111010111000010100011110101110000101000111101100让我觉得我做错了什么

基于http://www.binaryconvert.com/result_float.html?decimal=048046048055我应该0-01111011-00011110101110000101001但我不知道从哪里开始,二进制到十进制没有短划线?

如果有人可以帮助我,我们将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
function convert(){
    var num = parseFloat($("#inp").val(),10);
    
    var str = num.toString(2); // binary representation
console.log(num);
    //Normalize and find the exponent and mantissa
    var mantissa = parseInt(str.substring(0,str.indexOf(".")));
    var exp = 0;
   console.log(mantissa);
    if(mantissa <=0){
       var i = str.indexOf(".") +1;
       while(parseInt(str.charAt(i),10) < 1){
        i = i +1;
       } 
       exp = 127 - (i -1); //bias as 127;
       mantissa = str.substring(i+1);
    }
    
     else if(mantissa > 0){
       var i = str.indexOf(".");
       exp = i -1;        
       exp = 127 +exp; //bias as 127;
       mantissa = str.replace(".","").substring(1);
    }
   
    return "0 " + exp.toString(2).padStart(8,"0") + " "+mantissa;
    }
    
    $(document).ready(function(){
     // $("#result").text(convert());
      $("#inp").change(function(){
        $("#result").text(convert());
      });
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id ="inp"></input>
<div id ="result"></div>
&#13;
&#13;
&#13;