📄 ieee-754 floating-point conversion from 32-bit hexadecimal to floating-point.mht
字号:
if (outstring =3D=3D "")=0A=
{=0A=
this.StatCond =3D "overflow"=0A=
this.DispStr =3D "Infinity"=0A=
=0A=
if (this.Result[0] =3D=3D 1)=0A=
this.DispStr =3D "-" + this.DispStr=0A=
=0A=
if (this.Size =3D=3D 32) index2 =3D 9=0A=
else index2 =3D 12=0A=
=0A=
//zero the significand=0A=
while (index2 < this.Size)=0A=
{=0A=
this.Result[index2] =3D 0=0A=
index2++=0A=
}//while=0A=
=0A=
}//if overflowed=0A=
=0A=
else //already special values=0A=
{=0A=
this.StatCond =3D statstring=0A=
this.DispStr =3D outstring=0A=
}//if already special (else section)=0A=
=0A=
}//if max exponent=0A=
=0A=
//convert exponent value to binary representation=0A=
if (this.Size =3D=3D 32) index1 =3D 8=0A=
else index1 =3D 11=0A=
this.BinaryPower =3D binexpnt=0A=
binexpnt +=3D this.ExpBias //bias=0A=
while ((binexpnt / 2) !=3D 0)=0A=
{=0A=
this.Result[index1] =3D binexpnt % 2=0A=
if (binexpnt % 2 =3D=3D 0) binexpnt =3D binexpnt / 2=0A=
else binexpnt =3D binexpnt / 2 - 0.5=0A=
index1 -=3D 1=0A=
}=0A=
=0A=
//output binary result=0A=
output =3D ""=0A=
for (index1 =3D 0; index1 < this.Size; index1++) =0A=
output =3D output + this.Result[index1] =0A=
return output=0A=
=0A=
}//with Math=0A=
}=0A=
=0A=
function Hex2Bin(input)=0A=
{=0A=
output =3D new String() //Output=0A=
numerals =3D new String()=0A=
=0A=
var index1, nibble, i, s, binexpnt, cnst, bias, index2, zeroFirst, =
zeroRest=0A=
var binexpnt2, index3=0A=
=0A=
cnst =3D 2102 // 1 (carry bit) + 1023 + 1 + 1022 + 53 + 2 =
(round bits)=0A=
bias =3D 1024=0A=
=0A=
//init=0A=
numerals =3D "0123456789ABCDEF"=0A=
=0A=
for (index1 =3D 0; index1 < cnst; index1++) this.BinVal[index1] =3D 0=0A=
=0A=
for (index1 =3D 0; index1 < this.Size; index1++) this.Result[index1] =
=3D 0=0A=
=0A=
with (Math)=0A=
{=0A=
=0A=
input =3D RemoveBlanks(input)=0A=
=0A=
if (input.length > (this.Size / 4))=0A=
{=0A=
alert("too many hex digits")=0A=
output =3D "exit"=0A=
return output=0A=
}=0A=
=0A=
else if (input.length < (this.Size / 4))=0A=
{=0A=
alert("too few hex digits")=0A=
output =3D "exit"=0A=
return output=0A=
}=0A=
=0A=
else=0A=
{=0A=
input =3D input.toUpperCase()=0A=
=0A=
for (index1 =3D 0; index1 < this.Size; index1 +=3D4)=0A=
{=0A=
nibble =3D numerals.indexOf(input.charAt(index1 / 4))=0A=
=0A=
if (nibble =3D=3D -1)=0A=
{=0A=
alert("invalid hex digit")=0A=
output =3D "exit"=0A=
return output=0A=
}=0A=
=0A=
temp =3D nibble / 16=0A=
=0A=
for (i =3D 0; i < 4; i++)=0A=
{=0A=
temp *=3D 2=0A=
if (temp >=3D 1)=0A=
{=0A=
this.Result[index1 + i] =3D 1=0A=
temp --=0A=
}=0A=
else=0A=
this.Result[index1 + i] =3D 0=0A=
}=0A=
}=0A=
}=0A=
=0A=
//obtain exponent value=0A=
binexpnt =3D 0=0A=
=0A=
if (this.Size =3D=3D 32) index2 =3D 9=0A=
else index2 =3D 12=0A=
=0A=
for (index1 =3D 1; index1 < index2; index1++)=0A=
binexpnt +=3D parseInt(this.Result[index1])*pow(2, index2 - index1 =
- 1)=0A=
=0A=
binexpnt -=3D this.ExpBias //bias=0A=
this.BinaryPower =3D binexpnt=0A=
=0A=
index1 =3D bias - binexpnt=0A=
=0A=
//regular normalized numbers=0A=
if ((binexpnt >=3D this.MinExp) && (binexpnt <=3D this.MaxExp))=0A=
{=0A=
//the encoding's hidden 1 is inserted=0A=
this.BinVal[index1] =3D 1=0A=
index1++=0A=
}//if normalized numbers=0A=
=0A=
index3 =3D index1=0A=
=0A=
//copy the input=0A=
if (this.Result[index2] =3D=3D 0)=0A=
zeroFirst =3D true=0A=
this.BinVal[index1] =3D this.Result[index2]=0A=
index2++=0A=
index1++=0A=
=0A=
zeroRest =3D true=0A=
while ((index2 < this.Size) && (index1 < cnst))=0A=
{=0A=
if (this.Result[index2] =3D=3D 1)=0A=
zeroRest =3D false=0A=
this.BinVal[index1] =3D this.Result[index2]=0A=
index2++=0A=
index1++=0A=
}//while=0A=
=0A=
//find most significant bit of significand=0A=
//for the actual denormalized exponent test for zero=0A=
while ((index3 < cnst) && (this.BinVal[index3] !=3D 1)) index3++=0A=
binexpnt2 =3D bias - index3=0A=
=0A=
//zero and denormalized numbers=0A=
if (binexpnt < this.MinExp)=0A=
{=0A=
if (binexpnt2 =3D=3D bias - cnst)=0A=
//value is truely zero=0A=
this.StatCond =3D "normal"=0A=
else if (binexpnt2 < this.MinUnnormExp)=0A=
this.StatCond =3D "underflow"=0A=
else=0A=
this.StatCond =3D "denormalized"=0A=
}//if zero or denormalized=0A=
=0A=
//max exponent for this precision=0A=
else if (binexpnt > this.MaxExp)=0A=
{=0A=
if (zeroFirst && zeroRest)=0A=
{=0A=
//Infinity=0A=
this.StatCond =3D "overflow"=0A=
this.DispStr =3D "Infinity"=0A=
}//if Infinity=0A=
else if (!zeroFirst && zeroRest && (this.Result[0] =3D=3D 1))=0A=
{=0A=
//Indeterminate quiet NaN=0A=
this.StatCond =3D "quiet"=0A=
this.DispStr =3D "Indeterminate"=0A=
}//if Indeterminate quiet NaN (else if section)=0A=
else if (!zeroFirst)=0A=
{=0A=
//quiet NaN=0A=
this.StatCond =3D "quiet"=0A=
this.DispStr =3D "NaN"=0A=
}//if quiet NaN (else if section)=0A=
else=0A=
{=0A=
//signaling NaN=0A=
this.StatCond =3D "signaling"=0A=
this.DispStr =3D "NaN"=0A=
}//if signaling NaN (else section)=0A=
=0A=
if ((this.Result[0] =3D=3D 1) && (this.DispStr !=3D =
"Indeterminate"))=0A=
this.DispStr =3D "-" + this.DispStr=0A=
=0A=
}//if max exponent (else if section)=0A=
=0A=
//output binary result=0A=
output =3D ""=0A=
for (index1 =3D 0; index1 < this.Size; index1++)=0A=
output =3D output + this.Result[index1]=0A=
return output=0A=
=0A=
}//with Math=0A=
}=0A=
=0A=
function RemoveBlanks(input)=0A=
{=0A=
output =3D new String()=0A=
=0A=
var start, stop=0A=
=0A=
start =3D 0=0A=
while ((input.charAt(start) =3D=3D " ") && (start < input.length))=0A=
start++=0A=
=0A=
stop =3D input.length - 1=0A=
while ((input.charAt(stop) =3D=3D " ") && (stop >=3D 0))=0A=
stop--=0A=
=0A=
output =3D input.substring(start, stop + 1)=0A=
=0A=
return output=0A=
}=0A=
=0A=
function Convert2Hex()=0A=
{=0A=
output =3D new String()=0A=
numerals =3D new String()=0A=
=0A=
var temp, index, i=0A=
=0A=
numerals =3D "0123456789ABCDEF"=0A=
=0A=
with (Math)=0A=
{=0A=
//convert binary result to hex and output=0A=
for (index =3D 0; index < this.Size; index +=3D4)=0A=
{=0A=
temp =3D 0=0A=
for (i =3D 0; i < 4; i++)=0A=
temp +=3D pow(2, 3 - i)*this.Result[index + i]=0A=
=0A=
output =3D output + numerals.charAt(temp)=0A=
}=0A=
}=0A=
return output=0A=
}=0A=
=0A=
function numStrClipOff(input, precision)=0A=
{=0A=
result =3D new String()=0A=
numerals =3D new String()=0A=
tempstr =3D new String()=0A=
expstr =3D new String()=0A=
signstr =3D new String()=0A=
=0A=
var locE, stop, expnum, locDP, start, MSD, MSDfound, index, expdelta, =
digits=0A=
var number=0A=
=0A=
numerals =3D "0123456789";=0A=
=0A=
tempstr =3D input.toUpperCase()=0A=
=0A=
locE =3D tempstr.indexOf("E");=0A=
if (locE !=3D -1)=0A=
{=0A=
stop =3D locE=0A=
expstr =3D input.substring(locE + 1, input.length)=0A=
expnum =3D expstr * 1=0A=
}=0A=
else=0A=
{=0A=
stop =3D input.length=0A=
expnum =3D 0=0A=
}=0A=
=0A=
if (input.indexOf(".") =3D=3D -1)=0A=
{=0A=
tempstr =3D input.substring(0, stop)=0A=
tempstr +=3D "."=0A=
if (input.length !=3D stop)=0A=
tempstr +=3D input.substring(locE, input.length)=0A=
=0A=
input =3D tempstr=0A=
=0A=
locE =3D locE + 1=0A=
stop =3D stop + 1=0A=
}=0A=
=0A=
locDP =3D input.indexOf(".");=0A=
=0A=
start =3D 0=0A=
if (input.charAt(start) =3D=3D "-")=0A=
{=0A=
start++=0A=
signstr =3D "-"=0A=
}=0A=
else=0A=
signstr =3D ""=0A=
=0A=
MSD =3D start=0A=
MSDfound =3D false=0A=
while ((MSD < stop) && !MSDfound)=0A=
{=0A=
index =3D 1=0A=
while (index < numerals.length)=0A=
{=0A=
if (input.charAt(MSD) =3D=3D numerals.charAt(index))=0A=
{=0A=
MSDfound =3D true=0A=
break=0A=
}=0A=
index++=0A=
}=0A=
MSD++=0A=
}=0A=
MSD--=0A=
=0A=
if (MSDfound)=0A=
{=0A=
expdelta =3D locDP - MSD=0A=
if (expdelta > 0)=0A=
expdelta =3D expdelta - 1=0A=
=0A=
expnum =3D expnum + expdelta=0A=
=0A=
expstr =3D "e" + expnum=0A=
}=0A=
else //No significant digits found, value is zero=0A=
MSD =3D start=0A=
=0A=
digits =3D stop - MSD=0A=
=0A=
tempstr =3D input.substring(MSD, stop)=0A=
=0A=
if (tempstr.indexOf(".") !=3D -1)=0A=
digits =3D digits - 1=0A=
=0A=
number =3D digits=0A=
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -