📄 ch02_01.htm
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:29:57Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch01_19.htm"TITLE="1.18. Program: psgrep"><LINKREL="next"HREF="ch02_02.htm"TITLE="2.1. Checking Whether a String Is a Valid Number"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_19.htm"TITLE="1.18. Program: psgrep"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.18. Program: psgrep"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_02.htm"TITLE="2.1. Checking Whether a String Is a Valid Number"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.1. Checking Whether a String Is a Valid Number"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch02-69815">2. Numbers</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch02-39510"TITLE="2.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch02_02.htm"TITLE="2.1. Checking Whether a String Is a Valid Number">Checking Whether a String Is a Valid Number</A><BR><ACLASS="sect1"HREF="ch02_03.htm"TITLE="2.2. Comparing Floating-Point Numbers">Comparing Floating-Point Numbers</A><BR><ACLASS="sect1"HREF="ch02_04.htm"TITLE="2.3. Rounding Floating-Point Numbers">Rounding Floating-Point Numbers</A><BR><ACLASS="sect1"HREF="ch02_05.htm"TITLE="2.4. Converting Between Binary and Decimal">Converting Between Binary and Decimal</A><BR><ACLASS="sect1"HREF="ch02_06.htm"TITLE="2.5. Operating on a Series of Integers">Operating on a Series of Integers</A><BR><ACLASS="sect1"HREF="ch02_07.htm"TITLE="2.6. Working with Roman Numerals">Working with Roman Numerals</A><BR><ACLASS="sect1"HREF="ch02_08.htm"TITLE="2.7. Generating Random Numbers">Generating Random Numbers</A><BR><ACLASS="sect1"HREF="ch02_09.htm"TITLE="2.8. Generating Different Random Numbers">Generating Different Random Numbers</A><BR><ACLASS="sect1"HREF="ch02_10.htm"TITLE="2.9. Making Numbers Even More Random">Making Numbers Even More Random</A><BR><ACLASS="sect1"HREF="ch02_11.htm"TITLE="2.10. Generating Biased Random Numbers">Generating Biased Random Numbers</A><BR><ACLASS="sect1"HREF="ch02_12.htm"TITLE="2.11. Doing Trigonometry in Degrees, not Radians">Doing Trigonometry in Degrees, not Radians</A><BR><ACLASS="sect1"HREF="ch02_13.htm"TITLE="2.12. Calculating More Trigonometric Functions">Calculating More Trigonometric Functions</A><BR><ACLASS="sect1"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms">Taking Logarithms</A><BR><ACLASS="sect1"HREF="ch02_15.htm"TITLE="2.14. Multiplying Matrices">Multiplying Matrices</A><BR><ACLASS="sect1"HREF="ch02_16.htm"TITLE="2.15. Using Complex Numbers">Using Complex Numbers</A><BR><ACLASS="sect1"HREF="ch02_17.htm"TITLE="2.16. Converting Between Octal and Hexadecimal">Converting Between Octal and Hexadecimal</A><BR><ACLASS="sect1"HREF="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers">Putting Commas in Numbers</A><BR><ACLASS="sect1"HREF="ch02_19.htm"TITLE="2.18. Printing Correct Plurals">Printing Correct Plurals</A><BR><ACLASS="sect1"HREF="ch02_20.htm"TITLE="2.19. Program: Calculating Prime Factors">Program: Calculating Prime Factors</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.</I></P><PCLASS="attribution"ALIGN="right">- John von Neumann (1951)</P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch02-39510">2.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch02-idx-1000007972-0"></A>Numbers, the most basic data type of almost any programming language, can be surprisingly tricky. Random numbers, numbers with decimal points, series of numbers, and the conversion of strings to numbers all pose trouble.</P><PCLASS="para">Perl works hard to make life easy for you, and the facilities it provides for manipulating numbers are no exception to that rule. If you treat a scalar value as a number, Perl converts it to one. This means that when you read ages from a file, extract digits from a string, or acquire numbers from any of the other myriad textual sources that Real Life pushes your way, you don't need to jump through the hoops created by other languages' cumbersome requirements to turn an ASCII string into a number.</P><PCLASS="para">Perl tries its best to interpret a string as a number when you use it as one (such as in a mathematical expression), but it has no direct way of reporting that a string doesn't represent a valid number. Perl quietly converts non-numeric strings to zero, and it will stop converting the string once it reaches a non-numeric character - so "<CODECLASS="literal">A7</CODE>" is still <CODECLASS="literal">0</CODE>, and "<CODECLASS="literal">7A</CODE>" is just <CODECLASS="literal">7</CODE>. (Note, however, that the <BCLASS="emphasis.bold">-w</B> flag will warn of such improper conversions.) Sometimes (such as when validating input) you need to know if a string represents a valid number. We show you how in <ACLASS="xref"HREF="ch02_02.htm"TITLE="Checking Whether a String Is a Valid Number">Recipe 2.1</A>.</P><PCLASS="para"><ACLASS="xref"HREF="ch02_17.htm"TITLE="Converting Between Octal and Hexadecimal">Recipe 2.16</A> shows how to get a number from strings containing hexadecimal or octal representations of numbers like "<CODECLASS="literal">0xff</CODE>". Perl automatically converts literals in your program code (so <CODECLASS="literal">$a</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">3</CODE> <CODECLASS="literal">+</CODE> <CODECLASS="literal">0xff</CODE> will set <CODECLASS="literal">$a</CODE> to 258) but not data read by that program (you can't read "<CODECLASS="literal">0xff</CODE>" into <CODECLASS="literal">$b</CODE> and then say <CODECLASS="literal">$a</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">3</CODE> <CODECLASS="literal">+</CODE> <CODECLASS="literal">$b</CODE> to make <CODECLASS="literal">$a</CODE> become 258).</P><PCLASS="para"><ACLASS="indexterm"NAME="ch02-idx-1000008255-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008255-1"></A>As if integers weren't giving us enough grief, floating-point numbers can cause even more headaches. Internally, a computer represents numbers with decimal points as floating-point numbers in binary format. Floating-point numbers are not the same as real numbers; they are an approximation of real numbers, with limited precision. Although infinitely many real numbers exist, you only have finite space to represent them, usually about 64 bits or so. You have to cut corners to fit them all in.</P><PCLASS="para">When numbers are read from a file or appear as literals in your program, they are converted from decimal representation (e.g., 0.1) to internal representation. 0.1 can't be precisely represented as a binary floating-point number, just as 1/3 can't be exactly represented as a non-repeating decimal number. The computer's binary representation of 0.1, therefore, isn't exactly 0.1. To 20 decimal places, it is <CODECLASS="literal">0.10000000000000000555</CODE>.</P><PCLASS="para">Performing arithmetic on <ACLASS="indexterm"NAME="ch02-idx-1000007975-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000007975-1"></A>binary representations of floating-point numbers can accumulate errors in the representations. In the preceding example, <CODECLASS="literal">3</CODE> <CODECLASS="literal">*</CODE> <CODECLASS="literal">0.1</CODE> is not stored with the same bit pattern as <CODECLASS="literal">0.3</CODE>. This means you can't blindly test equality with <CODECLASS="literal">==</CODE> when you use Perl's floating-point numbers. Working with floating-point numbers is the subject of Recipes <ACLASS="xref"HREF="ch02_03.htm"TITLE="Comparing Floating-Point Numbers">Recipe 2.2</A> and <ACLASS="xref"HREF="ch02_04.htm"TITLE="Rounding Floating-Point Numbers">Recipe 2.3</A>.</P><PCLASS="para"><ACLASS="xref"HREF="ch02_05.htm"TITLE="Converting Between Binary and Decimal">Recipe 2.4</A> shows how to convert an ASCII string representing a binary number (e.g., "<CODECLASS="literal">1001</CODE>") into an integer (e.g., <CODECLASS="literal">9</CODE>) and back again. <ACLASS="xref"HREF="ch02_06.htm"TITLE="Operating on a Series of Integers">Recipe 2.5</A> gives three ways to perform one operation on each element of a set of consecutive integers. We show how to convert to and from Roman numerals in <ACLASS="xref"HREF="ch02_07.htm"TITLE="Working with Roman Numerals">Recipe 2.6</A>.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch02-idx-1000007976-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000007976-1"></A>Random numbers are the topic of several recipes. Perl's <CODECLASS="literal">rand</CODE> function returns a floating-point value between 0 and 1 or between 0 and its argument. We show how to get random numbers in a given range, how to make random numbers more randomly, and how to make <CODECLASS="literal">rand</CODE> give a different set of random numbers each time you run your program.</P><PCLASS="para">We round out the chapter with recipes on trigonometry, logarithms, matrix multiplication, complex numbers, and the often-asked question: "How do you put commas in numbers?"</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch01_19.htm"TITLE="1.18. Program: psgrep"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 1.18. Program: psgrep"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_02.htm"TITLE="2.1. Checking Whether a String Is a Valid Number"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.1. Checking Whether a String Is a Valid Number"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">1.18. Program: psgrep</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">2.1. Checking Whether a String Is a Valid Number</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -