📄 ch4.htm
字号:
18 <BR>
19 $fv = FutureValue();
<BR>
20 <BR>
21 print $fv;<BR>
22 <BR>
23 =cut<BR>
24 <BR>
25 @EXPORT = qw( FutureValue,
<BR>
26
PresentValue,<BR>
27
FVofAnnuity,<BR>
28
AnnuityOfFV,<BR>
29
getLastAverage,<BR>
30
getMovingAverage,<BR>
31
SetInterest);<BR>
32 <BR>
33 #<BR>
34 # Globals, if any<BR>
35 #<BR>
36 <BR>
37 local $defaultInterest
= 5.0;<BR>
38 <BR>
39 sub Finance::SetInterest($)
{<BR>
40
my $rate = shift(@_);<BR>
41
$defaultInterest = $rate;<BR>
42
printf "\n \$defaultInterest = $rate";<BR>
43 }<BR>
44 <BR>
45 # --------------------------------------------------------------------
<BR>
46 # Notes:<BR>
47 # 1. The interest
rate $r is given in a value of [0-100].<BR>
48 # 2. The $n given in the terms is the rate at which the
interest<BR>
49 # is
applied.<BR>
50 #<BR>
51 # --------------------------------------------------------------------
<BR>
52 <BR>
53 # --------------------------------------------------------------------
<BR>
54 # Present value of an investment given<BR>
55 # fv - a future
value<BR>
56 # r - rate per period<BR>
57 # n -
number of period<BR>
58 # --------------------------------------------------------------------
<BR>
59 sub Finance::FutureValue($$$)
{<BR>
60
my ($pv,$r,$n) = @_;<BR>
61
my $fv = $pv * ((1 + ($r/100)) ** $n);<BR>
62
return $fv;<BR>
63 }<BR>
64 <BR>
65 # --------------------------------------------------------------------
<BR>
66 # Present value of an investment given<BR>
67 # fv - a future
value<BR>
68 # r - rate per period<BR>
69 # n -
number of period<BR>
70 # --------------------------------------------------------------------
<BR>
71 sub Finance::PresentValue($$$)
{<BR>
72
my $pv;<BR>
73
my ($fv,$r,$n) = @_;<BR>
74
$pv = $fv / ((1 + ($r/100)) ** $n);<BR>
75
return $pv;<BR>
76 <BR>
77 }<BR>
78 <BR>
79 # --------------------------------------------------------------------
<BR>
80 # Get the future value of an annuity given<BR>
81 # mp - Monthly Payment
of Annuity<BR>
82 # r - rate per period<BR>
83 # n -
number of period<BR>
84 # --------------------------------------------------------------------
<BR>
85 <BR>
86 sub FVofAnnuity($$$) {<BR>
87
my $fv;<BR>
88
my $oneR;<BR>
89
my ($mp,$r,$n) = @_;<BR>
90 <BR>
91
$oneR = ( 1 + $r) ** $n;<BR>
92
$fv = $mp * ( ($oneR - 1)/ $r);<BR>
93
return $fv;<BR>
94 }<BR>
95 <BR>
96 # --------------------------------------------------------------------
<BR>
97 # Get the annuity
from the following bits of information<BR>
98 # r - rate per period<BR>
99 # n -
number of period<BR>
100 # fv - Future Value<BR>
101 # --------------------------------------------------------------------
<BR>
102 <BR>
103 sub AnnuityOfFV($$$) {<BR>
104
my $mp; # mp - Monthly Payment of Annuity<BR>
105
my $oneR;<BR>
106
my ($fv,$r,$n) = @_;<BR>
107 <BR>
108
$oneR = ( 1 + $r) ** $n;<BR>
109
$mp = $fv * ( $r/ ($oneR - 1));<BR>
110
return $mp;<BR>
111 }<BR>
112 <BR>
113 # --------------------------------------------------------------------
<BR>
114 # Get the average of the last "n" values in an array.
<BR>
115 # --------------------------------------------------------------------
<BR>
116 # The last $count number of elements from the array in @values
<BR>
117 # The total number of elements in @values is in $number<BR>
118 #<BR>
119 sub getLastAverage($$@) {<BR>
120
my ($count, $number, @values) = @_;<BR>
121
my $i;<BR>
122 <BR>
123
my $a = 0;<BR>
124
return 0 if ($count == 0);<BR>
125
for ($i = 0; $i< $count; $i++) {<BR>
126
$a += $values[$number - $i - 1];<BR>
127
}<BR>
128
return $a / $count;<BR>
129
}<BR>
130 <BR>
131 # --------------------------------------------------------------------
<BR>
132 # Get a moving average of the values.<BR>
133 # --------------------------------------------------------------------
<BR>
134 # The window size is the first parameter, the number of items
in the<BR>
135 # passed array is next. (This can easily be calculated within
the<BR>
136 # function using the scalar() function, but the subroutine
shown here<BR>
137 # is also being used to illustrate how to pass pointers.)
The reference to the<BR>
138 # array of values is passed next, followed by a reference
to the place<BR>
139 # the return values are to be stored.<BR>
140 #<BR>
141 sub getMovingAve($$\@\@) {<BR>
142
my ($count, $number, $values, $movingAve) = @_;<BR>
143
my $i;<BR>
144
my $a = 0;<BR>
145
my $v = 0;<BR>
146 <BR>
147
return 0 if ($count == 0);<BR>
148
return -1 if ($count > $number);<BR>
149
return -2 if ($count < 2);<BR>
150 <BR>
151
$$movingAve[0] = 0;<BR>
152
$$movingAve[$number - 1] = 0;<BR>
153
for ($i=0; $i<$count;$i++) {<BR>
154
$v = $$values[$i];<BR>
155
$a += $v / $count;<BR>
156
$$movingAve[$i] = 0;<BR>
157
}<BR>
158
for ($i=$count; $i<$number;$i++) {<BR>
159
$v = $$values[$i];<BR>
160
$a += $v / $count;<BR>
161
$v = $$values[$i - $count - 1];<BR>
162
$a -= $v / $count;<BR>
163
$$movingAve[$i] = $a;<BR>
164
}<BR>
165
return 0;<BR>
166
}<BR>
167 <BR>
168 1;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Look at the declaration of the function <TT><FONT FACE="Courier">FutureValue</FONT></TT>
with <TT><FONT FACE="Courier">($$$)</FONT></TT>. The three dollar
signs together signify three scalar numbers being passed into
the function. This extra scoping is present for validating the
type of the parameters passed into the function. If you were to
pass a string instead of a number into the function, you would
get a message very similar to this one:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Too many arguments for Finance::FutureValue
at ./f4.pl line 15, near "$time)"<BR>
Execution of ./f4.pl aborted due to compilation errors.</FONT></TT>
</BLOCKQUOTE>
<P>
The use of prototypes when defining functions prevents you from
sending in values other than what the function expects. Use <TT><FONT FACE="Courier">@</FONT></TT>
or <TT><FONT FACE="Courier">%</FONT></TT> to pass in an array
of values. If you are passing by reference, use <TT><FONT FACE="Courier">\@</FONT></TT>
or <TT><FONT FACE="Courier">\%</FONT></TT> to show a scalar reference
to an array or hash, respectively. If you do not use the backslash,
all other types in the argument list prototype are ignored. Other
types of disqualifiers include an ampersand for a reference to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -