📄 chap14.html
字号:
<!-- All material contained herein is copyright (c) McGraw-Hill Professional Books
All Rights Reserved. No use of this material may be made without express written
permission of the copyright holder. HTML conversions by Mega Space [barry@megaspace.com] -->
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Understanding Digital Signatures: Inside the Java Virtual Machine
by Bill Venners - Beta Version</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<TABLE BORDER="0" WIDTH="100%">
<TR><TD><A HREF="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/stores.html" target="bottom"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Orders"></A>
<IMG SRC="order_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/order_text.gif" WIDTH="103" HEIGHT="41" ALT="Orders"></TD>
<TD ALIGN="RIGHT"><A HREF="chap13.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap13.html"><IMG SRC="backward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/backward.gif" BORDER="0" ALT="Backward" WIDTH="32" HEIGHT="32"></A> <A HREF="chap15.html" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/venners/chap15.html"><IMG SRC="forward.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/forward.gif" BORDER="0" ALT="Forward" WIDTH="32" HEIGHT="32"></A></TD></TR>
<TR><TD COLSPAN="2"><A HREF="mailto:computing@mcgraw-hill.com"><IMG SRC="hotkey.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/hotkey.gif" ALIGN="LEFT" BORDER="0" WIDTH="40" HEIGHT="40" ALT="Comments"></A>
<IMG SRC="comment_text.gif" tppabs="http://www.pbg.mcgraw-hill.com/betabooks/images/comment_text.gif" WIDTH="73" HEIGHT="39" ALT="Comments"></TD></TR>
<TR><TD COLSPAN="2"><FONT FACE="ARIEL,HELVETICA" SIZE="-1"><I>© 1997 The McGraw-Hill Companies, Inc. All rights reserved. <BR>Any use of this Beta Book is subject to the rules stated in the <A HREF="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" tppabs="http://www.mcgraw-hill.com/corporate/news_info/copyrttm.htm" target="_top">Terms of Use</A>.</I></FONT><br>
<script language="javascript">
document.write("<a href='http://banners.linkbuddies.com/click.php?id=237296'><img src='http://banners.linkbuddies.com/image.php?id=237296&ref=" + document.referrer + "' width=468 height=60 alt='Click Here' border=0></a>");
</script></TD></TR>
</TABLE>
<HR>
<P><H1>Chapter Fourteen</H1></P>
<P><H2>Floating-Point Arithmetic</H2></P>
<P>This chapter describes the floating point numbers and the instructions that perform floating point arithmetic inside the Java Virtual Machine. The floating point numbers described here conform to the IEEE 754 floating point standard, which is the standard to which all Java Virtual Machine implementations must adhere.</P>
<P>Accompanying this chapter on the CD-ROM are two applets that interactively illustrate the material presented in the chapter. One applet, named <I>Inner Float</I>, allows you to view and manipulate the individual components that make up a floating point number. The other applet, named <I>Circle of Squares</I>, simulates the Java Virtual Machine executing a method that uses several of the floating point opcodes. Within this chapter, you will find descriptions of both of these applets.</P>
<I><P>Floating Point Numbers</P>
</I><P>The Java Virtual Machine's floating-point support adheres to the IEEE-754 1985 floating-point standard. This standard defines the format of 32-bit and 64-bit floating-point numbers and defines the operations upon those numbers. In the Java Virtual Machine, floating-point arithmetic is performed on 32-bit <FONT FACE="Courier New">float</FONT>s and 64-bit <FONT FACE="Courier New">double</FONT>s. For each opcode that performs arithmetic on <FONT FACE="Courier New">float</FONT>s, there is a corresponding opcode that performs the same operation on <FONT FACE="Courier New">double</FONT>s. </P>
<P>A floating-point number has four parts--a sign, a mantissa, a radix, and an exponent. The sign is either a 1 or -1. The mantissa, always a positive number, holds the significant digits of the floating-point number. The exponent indicates the positive or negative power of the radix that the mantissa and sign should be multiplied by. The four components are combined as follows to get the floating-point value: </P>
<P><FONT FACE="Courier New">begin math formula</FONT></P>
<P>sign * mantissa * radix <SUP>exponent</SUP> </P>
<P><FONT FACE="Courier New">end math forzmula</FONT></P>
<P>Floating-point numbers have multiple representations, because one can always multiply the mantissa of any floating-point number by some power of the radix and change the exponent to get the original number. For example, as shown in Table 14-1, the number -5 can be represented equally by any of the following forms in radix 10.</P>
<P>Table 14-1. <STRONG>Forms of -5</P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Sign</STRONG></TD><TD VALIGN="TOP"><STRONG>Mantissa</STRONG></TD><TD VALIGN="TOP"><STRONG>Radix <SUP>exponent</STRONG></TD></TR>
</SUP><TR><TD VALIGN="TOP">-1</TD><TD VALIGN="TOP">50</TD><TD VALIGN="TOP">10 <SUP>-1</TD></TR>
</SUP><TR><TD VALIGN="TOP">-1</TD><TD VALIGN="TOP">5</TD><TD VALIGN="TOP">10 <SUP>0</TD></TR>
</SUP><TR><TD VALIGN="TOP">-1</TD><TD VALIGN="TOP">0.5</TD><TD VALIGN="TOP">10 <SUP>1</TD></TR>
</SUP><TR><TD VALIGN="TOP">-1</TD><TD VALIGN="TOP">0.05</TD><TD VALIGN="TOP">10 <SUP>2</TD></TR>
</TABLE>
<P>For each floating-point number there is one representation that is said to be <EM>normalized.</EM> A floating-point number is normalized if its mantissa is within the range defined by the following relation: </P>
<P><FONT FACE="Courier New">begin math formula</FONT></P>
<P>1/radix <= mantissa < 1 </P>
<P><FONT FACE="Courier New">end math formula</FONT></P>
<P>A normalized radix 10 floating-point number has its decimal point just to the left of the first non-zero digit in the mantissa. The normalized floating-point representation of -5 is -1 * 0.5 * 10 <SUP>1</SUP>. In other words, a normalized floating-point number's mantissa has no non-zero digits to the left of the decimal point and a non-zero digit just to the right of the decimal point. Any floating-point number that doesn't fit into this category is said to be <EM>denormalized</EM>. Note that the number zero has no normalized representation, because it has no non-zero digit to put just to the right of the decimal point. "Why be normalized?" is a common exclamation among zeros. </P>
<P>Floating-point numbers in the Java Virtual Machine use a radix of two, so they have the following form: </P>
<P><FONT FACE="Courier New">begin math formula</FONT></P>
<P>sign * mantissa * 2 <SUP>exponent</SUP> </P>
<P><FONT FACE="Courier New">end math formula</FONT></P>
<P>The mantissa of a floating-point number in the Java Virtual Machine is expressed as a binary number. A normalized mantissa has its binary point (the base-two equivalent of a decimal point) just to the left of the most significant non-zero digit. Because the binary number system has just two digits--zero and one--the most significant digit of a normalized mantissa is always a one. </P>
<P>The most significant bit of a <FONT FACE="Courier New">float</FONT> or <FONT FACE="Courier New">double</FONT> is its sign bit. The mantissa occupies the 23 least significant bits of a <FONT FACE="Courier New">float</FONT> and the 52 least significant bits of a <FONT FACE="Courier New">double</FONT>. The exponent, 8 bits in a <FONT FACE="Courier New">float</FONT> and 11 bits in a <FONT FACE="Courier New">double</FONT>, sits between the sign and mantissa. The format of a <FONT FACE="Courier New">float</FONT> is shown below. The sign bit is shown as an "s," the exponent bits are shown as "e," and the mantissa bits are shown as "m": </P>
<P><FONT FACE="Courier New">begin math formula</FONT></P>
<P>s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm</P>
<P><FONT FACE="Courier New">end math formula</FONT></P>
<P>A sign bit of zero indicates a positive number and a sign bit of one indicates a negative number. The mantissa is always interpreted as a positive base-two number. It is not a twos-complement number. If the sign bit is one, the floating-point value is negative, but the mantissa is still interpreted as a positive number that must be multiplied by -1. </P>
<P>The exponent field is interpreted in one of three ways. An exponent of all ones indicates the floating-point number has one of the special values of plus or minus infinity, or "not a number" (NaN). NaN is the result of certain operations, such as the division of zero by zero. An exponent of all zeros indicates a denormalized floating-point number. Any other exponent indicates a normalized floating-point number. </P>
<P>The mantissa contains one extra bit of precision beyond those that appear in the mantissa bits. The mantissa of a <FONT FACE="Courier New">float</FONT>, which occupies only 23 bits, has 24 bits of precision. The mantissa of a <FONT FACE="Courier New">double</FONT>, which occupies 52 bits, has 53 bits of precision. The most significant mantissa bit is predictable, and is therefore not included, because the exponent of floating-point numbers in the Java Virtual Machine indicates whether or not the number is normalized. If the exponent is all zeros, the floating-point number is denormalized and the most significant bit of the mantissa is known to be a zero. Otherwise, the floating-point number is normalized and the most significant bit of the mantissa is known to be one. </P>
<P>The Java Virtual Machine throws no exceptions as a result of any floating-point operations. Special values, such as positive and negative infinity or NaN, are returned as the result of suspicious operations such as division by zero. An exponent of all ones indicates a special floating-point value. An exponent of all ones with a mantissa whose bits are all zero indicates an infinity. The sign of the infinity is indicated by the sign bit. An exponent of all ones with any other mantissa is interpreted to mean "not a number" (NaN). The Java Virtual Machine always produces the same mantissa for NaN, which is all zeros except for the most significant mantissa bit that appears in the number. These values are shown for a <FONT FACE="Courier New">float</FONT> in Table 14-2. </P>
<P>Table 14-2. <STRONG>Special <FONT FACE="Courier New">float</FONT> values</P>
</STRONG><TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Value</STRONG></TD><TD VALIGN="TOP"><STRONG><FONT FACE="Courier New">float</FONT> bits (sign exponent mantissa)</STRONG></TD></TR>
<TR><TD VALIGN="TOP">+Infinity</TD><TD VALIGN="TOP">0 11111111 00000000000000000000000</TD></TR>
<TR><TD VALIGN="TOP">-Infinity</TD><TD VALIGN="TOP">1 11111111 00000000000000000000000</TD></TR>
<TR><TD VALIGN="TOP">NaN</TD><TD VALIGN="TOP">1 11111111 10000000000000000000000</TD></TR>
</TABLE>
<P>Exponents that are neither all ones nor all zeros indicate the power of two by which to multiply the normalized mantissa. The power of two can be determined by interpreting the exponent bits as a positive number, and then subtracting a bias from the positive number. For a <FONT FACE="Courier New">float</FONT>, the bias is 126. For a <FONT FACE="Courier New">double</FONT>, the bias is 1023. For example, an exponent field in a <FONT FACE="Courier New">float</FONT> of 00000001 yields a power of two by subtracting the bias (126) from the exponent field interpreted as a positive integer (1). The power of two, therefore, is (1 - 126), which is -125. This is the smallest possible power of two for a <FONT FACE="Courier New">float</FONT>. At the other extreme, an exponent field of 11111110 yields a power of two of (254 - 126) or 128. The number 128 is the largest power of two available to a <FONT FACE="Courier New">float</FONT>. Several examples of normalized <FONT FACE="Courier New">float</FONT>s are shown in Table 14-3. </P>
<P>Table 14-3. <STRONG>Normalized <FONT FACE="Courier New">float</FONT> values</STRONG></P>
<TABLE WIDTH="500">
<TR><TD VALIGN="TOP"><STRONG>Value</STRONG></TD><TD VALIGN="TOP"><STRONG><FONT FACE="Courier New">float</FONT> bits (sign exponent mantissa)</STRONG></TD><TD VALIGN="TOP"><STRONG>Unbiased exponent</STRONG></TD></TR>
<TR><TD VALIGN="TOP">Largest positive (finite) <FONT FACE="Courier New">float</FONT></TD><TD VALIGN="TOP">0 11111110 11111111111111111111111</TD><TD VALIGN="TOP">128</TD></TR>
<TR><TD VALIGN="TOP">Largest negative (finite) <FONT FACE="Courier New">float</FONT></TD><TD VALIGN="TOP">1 11111110 11111111111111111111111</TD><TD VALIGN="TOP">128</TD></TR>
<TR><TD VALIGN="TOP">Smallest normalized <FONT FACE="Courier New">float</FONT></TD><TD VALIGN="TOP">1 00000001 00000000000000000000000</TD><TD VALIGN="TOP">-125</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -