?? ieee-754 floating-point conversion from floating-point to hexadecimal.mht
字號:
From: <Saved by Windows Internet Explorer 7>
Subject: IEEE-754 Floating-Point Conversion from Floating-Point to Hexadecimal
Date: Thu, 22 Nov 2007 17:45:32 +0530
MIME-Version: 1.0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://babbage.cs.qc.edu/IEEE-754/Decimal.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>IEEE-754 Floating-Point Conversion from =
Floating-Point to Hexadecimal</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<SCRIPT =3D"javascript">=0A=
/*=0A=
Copyright (c) 2003, City University of New York=0A=
All rights reserved.=0A=
=0A=
Redistribution and use in source and binary forms, with or=0A=
without modification, are permitted provided that the following=0A=
conditions are met:=0A=
=0A=
* Redistributions of source code must retain the above=0A=
* copyright notice, this list of conditions and the=0A=
* following disclaimer. Redistributions in binary form=0A=
* must reproduce the above copyright notice, this list of=0A=
* conditions and the following disclaimer in the=0A=
* documentation and/or other materials provided with the=0A=
* distribution. Neither the name of Queens College of CUNY=0A=
* nor the names of its contributors may be used to endorse=0A=
* or promote products derived from this software without=0A=
* specific prior written permission.=0A=
=0A=
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND=0A=
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,=0A=
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF=0A=
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE=0A=
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR=0A=
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,=0A=
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT=0A=
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;=0A=
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)=0A=
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN=0A=
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR=0A=
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,=0A=
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.=0A=
=0A=
Original version by Quanfei Wan, 1997=0A=
Modifications by Kevin J. Brewer, 1998/08/20 to 1998/09/21=0A=
=0A=
- Corrected exponential ranges to those specified in IEEE-754.=0A=
- Correction of decimal exponent field size.=0A=
- Made correction so that decimal significand is displayed even when=0A=
its value is 0.=0A=
- decBinVal array sizes corrected for small values (those near =
1.0*2**-126=0A=
for 32-bit or 1.0*2**-1022 for 64-bit).=0A=
- Corrected ieee32.Convert2Dec() to ieee64.Convert2Dec() in the ieee64=0A=
section.=0A=
- Added support for zero and denormalized numbers (those less than=0A=
1.0*2**-126 for 32-bit or 1.0*2**-1022 for 64-bit).=0A=
- Modified significand input field to hold the notationally largest =
range=0A=
input value (with the most significant digits, exponent, and signs),=0A=
-4.94065645841246544E-324 .=0A=
- Use only 1 set of intBinVal and decBinVal arrays so that 32-bit =
results=0A=
will be marked invalid when 64-bit values greater than 3.40282347E38=0A=
are entered.=0A=
- Joined arrays intBinVal and decBinVal into one array, BinVal, in =
order=0A=
to ease the manipulations involved in rounding.=0A=
=0A=
- Added IEEE-754 round-to-nearest value rounding mode.=0A=
=0A=
- Added input echo field which displays how the host machine sees the=0A=
input value (round-off, number of significant digits, max and min=0A=
exponentials, etc.).=0A=
- For both precisions, added an echo field which displays the input =
value=0A=
accurate to the number of bits in that precision's significand.=0A=
- Added action so that when an input value overflows a precision, set =
that=0A=
precision's outputs to the precision's Infinity values instead of =
having=0A=
the bits be replaced by "#"s.=0A=
- For both precisions, added a status field indicating from high to =
low:=0A=
overflow, normal, denormalized, underflow, and normal for zero.=0A=
- Run Convert2Bin() only once per precision (not for each substring).=0A=
- Added the ability to round or not to round by providing 2 activation=0A=
buttons labeled as such.=0A=
- Reduced exponent input field to hold up to the notationally largest=0A=
exponent value (with the most significant digits and sign), -324, =
unless=0A=
someone wants to oddly decimalize (add fractional/decimal parts to) =
the=0A=
exponent.=0A=
- Replaced calls to round() with tests and corrections for rounding =
up to=0A=
calls to floor() which always rounds down.=0A=
=0A=
1998/09/28 to 1998/09/30=0A=
=0A=
- Made BinVal external to Convert2Bin (and therefore local to =
function ieee)=0A=
as this.BinVal as is required in IEEE-754hex64.html and =
IEEE-754hex32.html=0A=
so that Unix 'diff' has some chance at comparing this file with=0A=
IEEE-754hex64.html or IEEE-754hex32.html .=0A=
- General clean-ups.=0A=
- Removed the optional "Exponent:" input field due to its problem of =
causing=0A=
the entire input to underflow to zero when its value is -324, the =
problems=0A=
encountered when trying to correct this deficiency programatically, =
and=0A=
its superfluousness due to the ability of entering scientific =
notation=0A=
(4.94065645841246544E-324) into the "Significand:" field alone.=0A=
- Renamed the "Significand:" input field to "Decimal Floating-Point:".=0A=
=0A=
1998/10/06 to 1998/10/07=0A=
=0A=
- Added removal of input leading and trailing blanks by adding =
RemoveBlanks=0A=
routine.=0A=
- Added converting all floating-point inputs into canonical form =
(scientific=0A=
notation, +x.xxE+xxxx form) with constant exponential width and =
plus sign=0A=
usage by adding the Canonical routine, in order to simplify the =
string=0A=
number comparisons involved in the 1.7976931348623157E+308 overflow =
check.=0A=
- Found and corrected bug in both binary outputs created by some =
unknown=0A=
JavaScript scoping problem for the symbol 'output' by introduction =
of the=0A=
Canonical routine (never in a released version).=0A=
=0A=
- Floating-point input is now hand parsed and then its magnitude is =
compared=0A=
against +1.7976931348623157E+00308 in the OvfCheck routine. If the=0A=
magnitude is greater than this value (compared as a string), set all=0A=
outputs to the appropriate Infinity values. The JavaScript =
implementation=0A=
of IEEE-754 64-bit precision clips such values at =
1.7976931348623157E+308.=0A=
=0A=
- Greatly improved the efficiency of the Convert2Hex routine.=0A=
=0A=
1998/10/20=0A=
=0A=
- Allow power of 10 indicator in numStrClipOff to be "E" as well as =
"e" in=0A=
case not all browsers use "e".=0A=
- Reordered 'numerals' in OvfCheck so that their index (value) is the =
same=0A=
as that which the numeral represents.=0A=
=0A=
1998/10/23=0A=
=0A=
- With hand parsed floating-point input, compare its magnitude against=0A=
+2.4703282292062328E-00324 in the UndfCheck routine. If the =
magnitude is=0A=
less than this value (compared as a string), set 'this.StatCond' to=0A=
"underflow". The JavaScript implementation of IEEE-754 64-bit =
precision=0A=
underflows such values to zero.=0A=
- The above required all settings of 'this.StatCond' to "normal" to =
have to=0A=
be removed, "normal" rather than "error" is now the default.=0A=
- With hand parsed input, set the output sign bit from its minus sign=0A=
character before the input is turned into a numeric. This supports =
input=0A=
of negative zeros, "-0", and those values which underflow to it.=0A=
=0A=
1998/10/28=0A=
=0A=
- The central testing section of OvfCheck and UndfCheck were joined =
together=0A=
as the single routine A_gt_B.=0A=
- The translation which moves an input's base 10 exponential (the =
sign and=0A=
value to the right of the "E") to its left end as its sign and most=0A=
significant digits is now performed by the MostSigOrder routine.=0A=
- Due to tests now involving negative exponentials in A_gt_B (via =
UndfCheck),=0A=
a bias of 50,000 is added to all exponentials, when moved by =
MostSigOrder,=0A=
in order to simplify numeric compares performed as iterative =
comparisons of=0A=
individual digits in strings.=0A=
=0A=
1998/10/29=0A=
=0A=
- Floating-point input is now hand parsed, manipulated, and placed =
into the=0A=
this.BinVal array only once by introduction of the Dec2Bin routine.=0A=
As a result, the Convert2Bin routine now used is quite similar to =
that of=0A=
IEEE-754hex64.html and IEEE-754hex32.html .=0A=
=0A=
1999/03/04=0A=
=0A=
- Corrected displaying error in numStrClipOff when the number of =
digits of=0A=
precision for a particular IEEE-754 format is less than the number =
of=0A=
digits returned by the system (before the value is large enough =
that the=0A=
system returns it in "E" notation).=0A=
Error found by Bill Maxwell (billmax@compuserve.com).=0A=
=0A=
1999/03/05=0A=
=0A=
- The system returns values such as 1.0E21 simply as 1E21. In =
numStrClipOff,=0A=
made adjustments to correct the display output when the system =
returns such=0A=
values.=0A=
Due to the idealized nature of the input to numStrClipOff vs. that =
of=0A=
Canonical, many simplifications to the code in numStrClipOff were =
made.=0A=
=0A=
- Added a Clear button next to the input field which clears the input =
field=0A=
and all result fields.=0A=
=0A=
1999/05/17=0A=
=0A=
- Removed <FONT FACE=3D"Arial"> which is not displayed the same by =
all browser=0A=
versions.=0A=
=0A=
- Balanced all <FONT> tags with </FONT> tags.=0A=
=0A=
- Removed all value layout comments since that information is much =
better=0A=
presented in the IEEE-754references.html file.=0A=
=0A=
- Aligned all "Bit xx" and "Bits xx - xx" headings.=0A=
=0A=
- Replaced all occurrences of the term "unnormalized" with the term=0A=
"denormalized" preferred by the standard.=0A=
=0A=
- Changed the "Decimal value of the exponent" display from "b + [e] =
=3D [f]"=0A=
to "[f] - b =3D [e]" where b =3D 127 or b =3D 1023=0A=
=0A=
- Headings "Exponent" changed to "Exponent Field" and headings=0A=
"Decimal value of the exponent" changed to=0A=
"Decimal value of exponent field and exponent".=0A=
=0A=
1999/05/28=0A=
=0A=
- Rounded the 32-bit decimal significand just like the 32-bit full =
decimal=0A=
value.=0A=
=0A=
- Fixed displaying problem in numStrClipOff in which values of=0A=
0.000000099... and smaller (in 32-bit) are displayed as 0.0000000 =
because=0A=
the values are not small enough for the system to return them in "E"=0A=
notation and similarly for 0.000000000000000099... and smaller in =
64-bit.=0A=
=0A=
*/=0A=
=0A=
function Convert2Bin(outstring, statstring, signBit, power, rounding)=0A=
{=0A=
output =3D new String() //Output=0A=
=0A=
var binexpnt, index1, index2, cnst, bias, lastbit, rounded, index3, =
binexpnt2=0A=
var moreBits=0A=
=0A=
cnst =3D 2102 // 1 (carry bit) + 1023 + 1 + 1022 + 53 + 2 (round =
bits)=0A=
bias =3D 1024=0A=
=0A=
//init=0A=
for (index1 =3D 0; index1 < this.Size; index1++) this.Result[index1] =
=3D 0=0A=
=0A=
with (Math)=0A=
{=0A=
//sign bit=0A=
this.Result[0] =3D signBit=0A=
=0A=
//obtain exponent value=0A=
index1 =3D 0=0A=
=0A=
if (this.Size =3D=3D 32) index2 =3D 9=0A=
else index2 =3D 12=0A=
=0A=
if (rounding && (statstring =3D=3D "normal"))=0A=
{=0A=
//find most significant bit of significand=0A=
while ((index1 < cnst) && (this.BinVal[index1] !=3D 1)) index1++=0A=
=0A=
binexpnt =3D bias - index1=0A=
=0A=
//regular normalized numbers=0A=
if (binexpnt >=3D this.MinExp)=0A=
{=0A=
//the value is shifted until the most=0A=
index1++ //significant 1 is to the left of the binary=0A=
//point and that bit is implicit in the encoding=0A=
}//if normalized numbers=0A=
=0A=
//support for zero and denormalized numbers=0A=
//exponent underflow for this precision=0A=
else=0A=
{=0A=
binexpnt =3D this.MinExp - 1=0A=
index1 =3D bias - binexpnt=0A=
}//if zero or denormalized (else section)=0A=
=0A=
=0A=
//use round to nearest value mode=0A=
=0A=
//compute least significant (low-order) bit of significand=0A=
lastbit =3D this.Size - 1 - index2 + index1=0A=
=0A=
//the bits folllowing the low-order bit have a value of (at least) =
1/2=0A=
if (this.BinVal[lastbit + 1] =3D=3D 1)=0A=
{=0A=
rounded =3D 0=0A=
=0A=
//odd low-order bit=0A=
if (this.BinVal[lastbit] =3D=3D 1)=0A=
{=0A=
//exactly 1/2 the way between odd and even rounds up to the =
even,=0A=
//so the rest of the bits don't need to be checked to see if =
the value=0A=
//is more than 1/2 since the round up to the even number will =
occur=0A=
//anyway due to the 1/2=0A=
rounded =3D 1=0A=
}//if odd low-order bit=0A=
=0A=
//even low-order bit=0A=
else //this.BinVal[lastbit] =3D=3D 0=0A=
{=0A=
//exactly 1/2 the way between even and odd rounds down to the =
even,=0A=
//so the rest of the bits need to be checked to see if the =
value=0A=
//is more than 1/2 in order to round up to the odd number=0A=
index3 =3D lastbit + 2=0A=
while ((rounded =3D=3D 0) && (index3 < cnst))=0A=
{=0A=
rounded =3D this.BinVal[index3]=0A=
index3++=0A=
}//while checking for more than 1/2=0A=
=0A=
}//if even low-order bit (else section)=0A=
=0A=
//do rounding "additions"=0A=
index3 =3D lastbit=0A=
while ((rounded =3D=3D 1) && (index3 >=3D 0))=0A=
{=0A=
// 0 + 1 -> 1 result with 0 carry=0A=
if (this.BinVal[index3] =3D=3D 0)=0A=
{=0A=
// 1 result=0A=
this.BinVal[index3] =3D 1=0A=
=0A=
// 0 carry=0A=
rounded =3D 0=0A=
=0A=
}//if bit is a 0=0A=
=0A=
// 1 + 1 -> 0 result with 1 carry=0A=
else //this.BinVal[index3] =3D=3D 1=0A=
{=0A=
// 0 result=0A=
this.BinVal[index3] =3D 0=0A=
=0A=
// 1 carry=0A=
// rounded =3D 1=0A=
}//if bit is a 1 (else section)=0A=
=0A=
index3--=0A=
}//while "adding" carries from right to left in bits=0A=
=0A=
}//if at least 1/2=0A=
=0A=
//obtain exponent value=0A=
index1 =3D index1 - 2=0A=
if (index1 < 0) index1 =3D 0=0A=
=0A=
}//if rounding=0A=
=0A=
//find most significant bit of significand=0A=
while ((index1 < cnst) && (this.BinVal[index1] !=3D 1)) index1++=0A=
=0A=
binexpnt2 =3D bias - index1=0A=
=0A=
if (statstring =3D=3D "normal")=0A=
{=0A=
binexpnt =3D binexpnt2=0A=
=0A=
//regular normalized numbers=0A=
if ((binexpnt >=3D this.MinExp) && (binexpnt <=3D this.MaxExp))=0A=
{=0A=
//the value is shifted until the most=0A=
index1++ //significant 1 is to the left of the =
binary=0A=
//point and that bit is implicit in the =
encoding=0A=
}//if normalized numbers=0A=
=0A=
//support for zero and denormalized numbers=0A=
//exponent underflow for this precision=0A=
else 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=
=0A=
binexpnt =3D this.MinExp - 1=0A=
index1 =3D bias - binexpnt=0A=
}//if zero or denormalized (else if section)=0A=
}=0A=
=0A=
else //already special values=0A=
{=0A=
binexpnt =3D power=0A=
index1 =3D bias - binexpnt=0A=
=0A=
if (binexpnt > this.MaxExp)=0A=
binexpnt =3D this.MaxExp + 1=0A=
=0A=
else if (binexpnt < this.MinExp)=0A=
binexpnt =3D this.MinExp - 1=0A=
=0A=
}//if already special (else section)=0A=
=0A=
//copy the result=0A=
while ((index2 < this.Size) && (index1 < cnst))=0A=
{=0A=
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -