如何从 Javascript 中的符号、尾数和指数中获取小数
How to Get Decimal from Sign, Mantissa, and Exponent in Javascript
我正在尝试获取 decimal/float 的 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
但我不知道我会从那里去哪里,没有破折号的二进制到十进制?
如果有人能帮助我,我将不胜感激!谢谢!
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());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id ="inp"></input>
<div id ="result"></div>
我正在尝试获取 decimal/float 的 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
但我不知道我会从那里去哪里,没有破折号的二进制到十进制?
如果有人能帮助我,我将不胜感激!谢谢!
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());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id ="inp"></input>
<div id ="result"></div>