如何在Javascript中从符号、尾数和指数中获取小数

如何在Javascript中从符号、尾数和指数中获取小数,javascript,ieee-754,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

我正在尝试获取十进制/浮点的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);
所以我做了
toBinary(Math.pow(2,exponent))
toBinary(尾数)
现在就开始
0-100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000-1111010111000010100011110101110000100001100110011101100
这让我觉得自己做错了什么

基于此,我应该得到
0-011111011-000111110111000010001
,但我不知道从那里我会走到哪里,从二进制到十进制,没有破折号

如果有人能帮助我,我将不胜感激!谢谢

函数转换(){
var num=parseFloat($(“#inp”).val(),10);
var str=num.toString(2);//二进制表示
console.log(num);
//归一化并找到指数和尾数
var尾数=parseInt(str.substring(0,str.indexOf(“.”));
var-exp=0;
控制台日志(尾数);
如果(尾数0){
var i=str.indexOf(“.”);
exp=i-1;
exp=127+exp;//偏差为127;
尾数=str.replace(“.”,“”)。子串(1);
}
返回“0”+exp.toString(2).padStart(8,“0”)+“”+尾数;
}
$(文档).ready(函数(){
//$(“#结果”).text(convert());
$(“#inp”).change(函数(){
$(“#结果”).text(convert());
});
});

@OliverCharlesworth这就是我大部分代码的来源!我已经看完了所有的答案,但并没有一个给了我所有需要的。我相信我在某个地方犯了一个错误,但我没能在那篇文章中找到它。如果你看到了我忽略的东西,请告诉我!谢谢
function toBinary (decimal) {
    return decimal.toString(2);
}