⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02_02.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Numbers (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch02_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.2. Numbers</h2><p><a name="INDEX-112" /><a name="INDEX-113" />Although a scalar is mostoften either a number or a string, it's useful to look atnumbers and strings separately for the moment. We'll covernumbers first, and then move on to strings.</p><a name="lperl3-CHP-2-SECT-2.1" /><div class="sect2"><h3 class="sect2">2.2.1. All Numbers Are the Same Format Internally</h3><p>As you'll see in the next few paragraphs, you can specify both<a name="INDEX-114" />integers (wholenumbers, like 255 or 2001) and floating-point numbers (real numberswith decimal points, like 3.14159, or 1.35 x 1025). Butinternally, Perl computes with <a name="INDEX-115" />double-precisionfloating-point values.<a href="#FOOTNOTE-42">[42]</a> This means that thereare no integer values <a name="INDEX-116" />internal to Perl -- an integerconstant in the program is treated as the equivalent floating-pointvalue.<a href="#FOOTNOTE-43">[43]</a> You probably won't notice the conversion (or caremuch), but you should stop looking for distinct integer operations(as opposed to <em class="emphasis">floating-point</em> operations),because there aren't any.<a href="#FOOTNOTE-44">[44]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-42" /><p>[42]A double-precisionfloating-point value is whatever the C compiler that compiled Perlused for a <tt class="literal">double</tt> declaration. While the size mayvary from machine to machine, most modern systems use IEEEfloating-point formats, which suggest 15 digits of precision and arange of at least <tt class="literal">1e-100</tt> to<tt class="literal">1e100</tt>.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-43" /><p>[43]Well, Perl will sometimes use internalintegers in ways that are not visible to the programmer. That is, theonly difference you should generally be able to see is that yourprogram runs faster. And who could complain about that?</p></blockquote><blockquote class="footnote"> <a name="FOOTNOTE-44" /><p>[44]Okay, there is the<tt class="literal">integer</tt> <a name="INDEX-117" /><a name="INDEX-118" />pragma.But using that is beyond the scope of this book. And yes, someoperations force an integer to be computed from a givenfloating-point number, as we'll see later. But that's notwhat we're talking about here.</p> </blockquote></div><a name="lperl3-CHP-2-SECT-2.2" /><div class="sect2"><h3 class="sect2">2.2.2. Floating-Point Literals</h3><p>A <a name="INDEX-119" />literal is theway a value is represented in the source code of the Perl program. Aliteral is not the result of a calculation or an I/O operation;it's data written directly into the source code.</p><p>Perl's <a name="INDEX-120" />floating-point literals should lookfamiliar to you. Numbers with and without decimal points are allowed(including an optional plus or minus prefix), as well as tacking on apower-of-10 indicator (exponential notation) with E notation. Forexample:</p><blockquote><pre class="code">1.25255.000255.07.25e45  # 7.25 times 10 to the 45th power (a big number)-6.5e24  # negative 6.5 times 10 to the 24th         # (a big negative number)-12e-24  # negative 12 times 10 to the -24th         # (a very small negative number)-1.2E-23 # another way to say that - the E may be uppercase</pre></blockquote></div><a name="lperl3-CHP-2-SECT-2.3" /><div class="sect2"><h3 class="sect2">2.2.3. Integer Literals</h3><p><a name="INDEX-121" />Integerliterals are also straightforward, as in:</p><blockquote><pre class="code">02001-4025561298040283768</pre></blockquote><p>That last one is a little hard to read. Perl allows<a name="INDEX-122" /><a name="INDEX-123" />underscores for clarity withininteger literals, so we can also write that number like this:</p><blockquote><pre class="code">61_298_040_283_768</pre></blockquote><p>It's the same value; it merely looks different to us humanbeings. You might have thought that commas should be used for thispurpose, but commas are already used for a more-important purpose inPerl (as we'll see in the next chapter).</p></div><a name="lperl3-CHP-2-SECT-2.4" /><div class="sect2"><h3 class="sect2">2.2.4. Nondecimal Integer Literals</h3><p><a name="INDEX-124" />Like many other programming languages,Perl allows you to specify numbers in other than base 10 (decimal).<a name="INDEX-125" />Octal (base8) literals start with a <a name="INDEX-126" />leading<tt class="literal">0</tt>,<a name="INDEX-127" /> hexadecimal (base 16) literals startwith a leading <tt class="literal">0x</tt>, and binary (base 2) literalsstart with a leading <tt class="literal">0b</tt>.<a href="#FOOTNOTE-45">[45]</a> Thehex digits <tt class="literal">A</tt> through <tt class="literal">F</tt> (or<tt class="literal">a</tt> through <tt class="literal">f</tt>) represent theconventional digit values of ten through fifteen. For example:</p><blockquote class="footnote"> <a name="FOOTNOTE-45" /><p>[45]The"leading zero" indicator works only forliterals -- not for automatic string-to-number conversion, whichwe'll see later in this chapter. You can convert a data stringthat looks like an octal or hex value into a number with<tt class="literal">oct( )</tt>or <tt class="literal">hex( )</tt>. Althoughthere's no "<tt class="literal">bin</tt>" function forconverting binary values, <tt class="literal">oct( )</tt>can do that forstrings beginning with <tt class="literal">0b</tt>.</p> </blockquote><blockquote><pre class="code">0377       # 377 octal, same as 255 decimal0xff       # FF hex, also 255 decimal0b11111111 # also 255 decimal (available in version 5.6 and later)</pre></blockquote><p>Although these values look different to us humans, they're allthree the same number to Perl. It makes no difference to Perl whetheryou write <tt class="literal">0xFF</tt> or <tt class="literal">255.000</tt>, sochoose the representation that makes the most sense to you and yourmaintenance programmer (by which we mean the poor chap who gets stucktrying to figure out what you meant when you wrote your code. Mostoften, this poor chap is you, and you can't recall whay you didwhat you did three months ago).</p><p>When a non-decimal literal is more than about four characters long,it may be hard to read. For this reason, starting in version 5.6,Perl allows underscores for clarity within these literals:</p><blockquote><pre class="code">0x1377_0b770x50_65_72_7C</pre></blockquote></div><a name="lperl3-CHP-2-SECT-2.5" /><div class="sect2"><h3 class="sect2">2.2.5. Numeric Operators</h3><p><a name="INDEX-128" /><a name="INDEX-129" />Perlprovides the typical ordinary <a name="INDEX-130" /> <a name="INDEX-131" />addition, <a name="INDEX-132" /> <a name="INDEX-133" />subtraction, <a name="INDEX-134" /><a name="INDEX-135" />multiplication, and <a name="INDEX-136" /> <a name="INDEX-137" />division operators, and so on.For example:</p><blockquote><pre class="code">2 + 3      # 2 plus 3, or 55.1 - 2.4  # 5.1 minus 2.4, or 2.73 * 12     # 3 times 12 = 3614 / 2     # 14 divided by 2, or 710.2 / 0.3 # 10.2 divided by 0.3, or 3410 / 3     # always floating-point divide, so 3.3333333...</pre></blockquote><p>Perl also supports a<em class="emphasis">modulus</em><a name="INDEX-138" /> <a name="INDEX-139" /> operator (<tt class="literal">%</tt>). The valueof the expression <tt class="literal">10 % 3</tt> is the remainder when tenis divided by three, which is one. Both values are first reduced totheir integer values, so <tt class="literal">10.5 % 3.2</tt> is computed as<tt class="literal">10 % 3</tt>.<a href="#FOOTNOTE-46">[46]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-46" /><p>[46]The result of a modulusoperator when a negative number (or two) is involved can vary betweenPerl implementations. Beware.</p> </blockquote><p>Additionally, Perl provides the <span class="acronym">FORTRAN</span>-like<em class="emphasis">exponentiation</em><a name="INDEX-140" /><a name="INDEX-141" /> operator, which many have yearned for inPascal and C. The operator is represented by the double asterisk,such as <tt class="literal">2**3</tt>, which is two to the third power, oreight.<a href="#FOOTNOTE-47">[47]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-47" /><p>[47]You can't normally raise a negativenumber to a noninteger exponent. Math geeks know that the resultwould be a complex number. To make that possible, you'll needthe help of the <tt class="literal">Math::Complex</tt> module.</p></blockquote><p>In addition, there are other numeric operators, which we'llintroduce as we need them.<a name="INDEX-142" /> </p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_01.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch02_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2. Scalar Data</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">2.3. Strings</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; 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 + -