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

📄 numbers.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Numbers
Date: Wed, 29 Apr 2009 09:25:33 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/numbers.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Numbers</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3DStrings =
href=3D"strings.html"=20
rel=3DPREVIOUS><LINK title=3D"Dates and Times" =
href=3D"datesandtimes.html"=20
rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/strings.html">Prev</A></T=
D>
    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/datesandtimes.html">Next<=
/A></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DNUMBERS>2. Numbers</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN73>Checking Whether a String Is a Valid=20
Number</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$string =3D~ /PATTERN/<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># is a number
</FONT><FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># is not
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"has =
nondigits"</FONT>        <FONT color=3D#ffa500>if</FONT>     /\D/<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"not a natural =
number"</FONT> <FONT color=3D#ffa500>unless</FONT> /^\d+$/<FONT =
color=3D#00ffff>;</FONT>             <FONT color=3D#bebebe># rejects -3
</FONT><FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"not an =
integer"</FONT>       <FONT color=3D#ffa500>unless</FONT> /^-?\d+$/<FONT =
color=3D#00ffff>;</FONT>           <FONT color=3D#bebebe># rejects +3
</FONT><FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"not an =
integer"</FONT>       <FONT color=3D#ffa500>unless</FONT> /^<FONT =
color=3D#00ffff>[</FONT>+-<FONT color=3D#00ffff>]</FONT>?\d+$/<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"not a decimal =
number"</FONT> <FONT color=3D#ffa500>unless</FONT> /^-?\d+\.?\d*$/<FONT =
color=3D#00ffff>;</FONT>     <FONT color=3D#bebebe># rejects .2
</FONT><FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"not a =
decimal number"</FONT> <FONT color=3D#ffa500>unless</FONT> /^-?<FONT =
color=3D#00ffff>(</FONT>?:\d+<FONT color=3D#00ffff>(</FONT>?:\.\d*<FONT =
color=3D#00ffff>)</FONT>?|\.\d+<FONT color=3D#00ffff>)</FONT>$/<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>warn</FONT> <FONT color=3D#00cd00>"not a C =
float"</FONT>
       <FONT color=3D#ffa500>unless</FONT> /^<FONT =
color=3D#00ffff>([</FONT>+-<FONT color=3D#00ffff>]</FONT>?<FONT =
color=3D#00ffff>)(</FONT>?=3D\d|\.\d<FONT =
color=3D#00ffff>)</FONT>\d*<FONT color=3D#00ffff>(</FONT>\.\d*<FONT =
color=3D#00ffff>)</FONT>?<FONT color=3D#00ffff>([</FONT>Ee<FONT =
color=3D#00ffff>]([</FONT>+-<FONT color=3D#00ffff>]</FONT>?\d+<FONT =
color=3D#00ffff>))</FONT>?$/<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>getnum</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>use</FONT> <FONT color=3D#b2dfee>POSIX</FONT> =
<FONT color=3D#ff7f50>qw</FONT><FONT color=3D#00ffff>(</FONT>strtod<FONT =
color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$str</FONT> =
=3D <FONT color=3D#ff7f50>shift</FONT><FONT color=3D#00ffff>;</FONT>
    $str =3D~ <FONT color=3D#ff7f50>s</FONT>/^\s+//<FONT =
color=3D#00ffff>;</FONT>
    $str =3D~ <FONT color=3D#ff7f50>s</FONT>/\s+$//<FONT =
color=3D#00ffff>;</FONT>
    $! =3D <FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#9ac0cd>$num</FONT>, <FONT color=3D#9ac0cd>$unparsed</FONT><FONT =
color=3D#00ffff>)</FONT> =3D strtod<FONT =
color=3D#00ffff>(</FONT>$str<FONT color=3D#00ffff>);</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>((</FONT>$str =
<FONT color=3D#98fb98>eq</FONT> <FONT color=3D#00cd00>''</FONT><FONT =
color=3D#00ffff>)</FONT> || <FONT color=3D#00ffff>(</FONT>$unparsed !=3D =
<FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>)</FONT> || $!<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ffa500>return</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> =
<FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ffa500>return</FONT> $num<FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>is_numeric</FONT> =
<FONT color=3D#00ffff>{</FONT> <FONT color=3D#ff7f50>defined</FONT> =
<FONT color=3D#ff7f50>scalar</FONT> &amp;getnum <FONT =
color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN76>Comparing Floating-Point =
Numbers</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
# equal(NUM1, NUM2, ACCURACY) : returns true if NUM1 and NUM2 are
# equal to ACCURACY number of decimal places
</FONT>
<FONT color=3D#ffa500>sub</FONT> <FONT color=3D#b2dfee>equal</FONT> =
<FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#9ac0cd>$A</FONT>, <FONT color=3D#9ac0cd>$B</FONT>, <FONT =
color=3D#9ac0cd>$dp</FONT><FONT color=3D#00ffff>)</FONT> =3D <FONT =
color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>;</FONT>

    <FONT color=3D#ffa500>return</FONT> <FONT =
color=3D#98fb98>sprintf</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"%.${dp}g"</FONT>, $A<FONT color=3D#00ffff>)</FONT> =
<FONT color=3D#98fb98>eq</FONT> <FONT =
color=3D#98fb98>sprintf</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"%.${dp}g"</FONT>, $B<FONT color=3D#00ffff>);</FONT>
  <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$wage =3D <FONT color=3D#cdcd00>536</FONT><FONT =
color=3D#00ffff>;</FONT>                <FONT color=3D#bebebe># =
$5.36/hour
</FONT>$week =3D <FONT color=3D#cdcd00>40</FONT> * $wage<FONT =
color=3D#00ffff>;</FONT>         <FONT color=3D#bebebe># $214.40
</FONT><FONT color=3D#ff7f50>printf</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"One week's wage is: =
\$%.2f\n"</FONT>, $week/<FONT color=3D#cdcd00>100</FONT><FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#
#One week's wage is: $214.40
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN79>Rounding Floating-Point =
Numbers</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$rounded =3D <FONT color=3D#98fb98>sprintf</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"</FONT><FONT =
color=3D#cdcd00>%FORMATf</FONT><FONT color=3D#00cd00>"</FONT>, =
$unrounded<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$a =3D <FONT color=3D#cdcd00>0.255</FONT><FONT =
color=3D#00ffff>;</FONT>
$b =3D <FONT color=3D#98fb98>sprintf</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"%.2f"</FONT>, $a<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Unrounded: =
$a\nRounded: $b\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>printf</FONT> <FONT color=3D#00cd00>"Unrounded: =
$a\nRounded: %.2f\n"</FONT>, $a<FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe># Unrounded: 0.255
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Rounded: 0.26
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Unrounded: 0.255
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Rounded: 0.26
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>POSIX</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"number\tint\tfloor\tceil\n"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdad00>@a</FONT> =3D <FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#cdcd00>3.3</FONT> , <FONT color=3D#cdcd00>3.5</FONT> , <FONT =
color=3D#cdcd00>3.7</FONT>, <FONT color=3D#cdcd00>-3.3</FONT> <FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@a</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>printf</FONT><FONT color=3D#00ffff>(</FONT> =
<FONT color=3D#00cd00>"%.1f\t%.1f\t%.1f\t%.1f\n"</FONT>,<FONT =
color=3D#43cd80> </FONT>
        $_, <FONT color=3D#98fb98>int</FONT><FONT =
color=3D#00ffff>(</FONT>$_<FONT color=3D#00ffff>)</FONT>, floor<FONT =
color=3D#00ffff>(</FONT>$_<FONT color=3D#00ffff>)</FONT>, ceil<FONT =
color=3D#00ffff>(</FONT>$_<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe># number  int     floor   ceil
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#  3.3     3.0     3.0     4.0
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#  3.5     3.0     3.0     4.0
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#  3.7     3.0     3.0     4.0
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# -3.3    -3.0    -4.0    -3.0
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN82>Converting Between Binary and =
Decimal</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>dec2bin</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$str</FONT> =
=3D <FONT color=3D#98fb98>unpack</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"B32"</FONT>, <FONT =
color=3D#98fb98>pack</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"N"</FONT>, <FONT color=3D#ff7f50>shift</FONT><FONT =
color=3D#00ffff>));</FONT>
    $str =3D~ <FONT color=3D#ff7f50>s</FONT>/^<FONT =
color=3D#cdcd00>0</FONT>+<FONT color=3D#00ffff>(</FONT>?=3D\d<FONT =
color=3D#00ffff>)</FONT>//<FONT color=3D#00ffff>;</FONT>   <FONT =
color=3D#bebebe># otherwise you'll get leading zeros
</FONT>    <FONT color=3D#ffa500>return</FONT> $str<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>bin2dec</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT =
color=3D#98fb98>unpack</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"N"</FONT>, <FONT color=3D#98fb98>pack</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"B32"</FONT>, <FONT =
color=3D#98fb98>substr</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"0"</FONT> <FONT color=3D#98fb98>x</FONT> <FONT =
color=3D#cdcd00>32</FONT> . <FONT color=3D#ff7f50>shift</FONT>, <FONT =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -