| … | |
… | |
| 74 | - floor(-$x); |
74 | - floor(-$x); |
| 75 | } |
75 | } |
| 76 | sub floor { |
76 | sub floor { |
| 77 | my $input = shift; |
77 | my $input = shift; |
| 78 | my $out = int $input; |
78 | my $out = int $input; |
| 79 | $out -- if ( $out <= 0 and $out-$input); # does the right thing for negative numbers |
79 | $out -- if ( $out <= 0 and ($out-$input) > 0 ); # does the right thing for negative numbers |
| 80 | $out; |
80 | $out; |
| 81 | } |
81 | } |
| 82 | |
82 | |
| 83 | sub max { |
83 | sub max { |
| 84 | |
84 | |
| 85 | my $maxVal = shift; |
85 | my $maxVal = shift; |
| 86 | my @input = @_; |
86 | my @input = @_; |
| 87 | |
87 | |
| 88 | foreach my $num (@input) { |
88 | foreach my $num (@input) { |
| 89 | $maxVal = $num if ($maxVal < $num); |
89 | $maxVal = $num if ($maxVal < $num); |
| 90 | } |
90 | } |
| 91 | |
91 | |
| 92 | $maxVal; |
92 | $maxVal; |
| 93 | |
93 | |
| 94 | } |
94 | } |
| 95 | |
95 | |
| 96 | sub min { |
96 | sub min { |
| 97 | |
97 | |
| 98 | my $minVal = shift; |
98 | my $minVal = shift; |
| 99 | my @input = @_; |
99 | my @input = @_; |
| 100 | |
100 | |
| 101 | foreach my $num (@input) { |
101 | foreach my $num (@input) { |
| 102 | $minVal = $num if ($minVal > $num); |
102 | $minVal = $num if ($minVal > $num); |
| 103 | } |
103 | } |
| 104 | |
104 | |
| 105 | $minVal; |
105 | $minVal; |
| 106 | |
106 | |
| 107 | } |
107 | } |
| 108 | |
108 | |
| 109 | #round added 6/12/2000 by David Etlinger |
109 | #round added 6/12/2000 by David Etlinger |
| 110 | sub round { |
110 | sub round { |
| 111 | my $input = shift; |
111 | my $input = shift; |
| 112 | my $out; |
112 | my $out; |
| … | |
… | |
| 147 | |
147 | |
| 148 | # greatest common factor |
148 | # greatest common factor |
| 149 | # takes in two scalar values and uses the Euclidean Algorithm to return the gcf |
149 | # takes in two scalar values and uses the Euclidean Algorithm to return the gcf |
| 150 | #VS 6/29/2000 |
150 | #VS 6/29/2000 |
| 151 | |
151 | |
| 152 | sub gcf { |
152 | sub gcf { |
| 153 | my $a = abs(shift); # absolute values because this will yield the same gcd, |
153 | my $a = abs(shift); # absolute values because this will yield the same gcd, |
| 154 | my $b = abs(shift); # but allows use of the mod operation |
154 | my $b = abs(shift); # but allows use of the mod operation |
| 155 | |
155 | |
| 156 | # reorder such that b is the smaller number |
156 | # reorder such that b is the smaller number |
| 157 | if ($a < $b) { |
157 | if ($a < $b) { |
| 158 | my $temp = $a; |
158 | my $temp = $a; |
| 159 | $a = $b; |
159 | $a = $b; |
| 160 | $b = $temp; |
160 | $b = $temp; |
| 161 | } |
161 | } |
| 162 | |
162 | |
| 163 | return $a if $b == 0; |
163 | return $a if $b == 0; |
| 164 | |
164 | |
| 165 | my $q = int($a/$b); # quotient |
165 | my $q = int($a/$b); # quotient |
| 166 | my $r = $a % $b; # remainder |
166 | my $r = $a % $b; # remainder |
| 167 | |
167 | |
| 168 | return $b if $r == 0; |
168 | return $b if $r == 0; |
| 169 | |
169 | |
| 170 | my $tempR = $r; |
170 | my $tempR = $r; |
| 171 | |
171 | |
| 172 | while ($r != 0) { |
172 | while ($r != 0) { |
| 173 | |
173 | |
| 174 | #keep track of what $r was in the last loop, as this is the value |
174 | #keep track of what $r was in the last loop, as this is the value |
| 175 | #we will want when $r is set to 0 |
175 | #we will want when $r is set to 0 |
| 176 | $tempR = $r; |
176 | $tempR = $r; |
| 177 | |
177 | |
| 178 | $a = $b; |
178 | $a = $b; |
| 179 | $b = $r; |
179 | $b = $r; |
| … | |
… | |
| 220 | my @frac = ($num, $denom); |
220 | my @frac = ($num, $denom); |
| 221 | @frac; |
221 | @frac; |
| 222 | } |
222 | } |
| 223 | |
223 | |
| 224 | |
224 | |
| 225 | # takes a number and fixed object, as in "$a x" and formats |
225 | # takes a number and fixed object, as in "$a x" and formats |
| 226 | # to account for when $a = 0, 1, -1 |
226 | # to account for when $a = 0, 1, -1 |
| 227 | # Usage: format($scalar, "quoted string"); |
227 | # Usage: format($scalar, "quoted string"); |
| 228 | # Example: format(-1, "\pi") returns "-\pi" |
228 | # Example: format(-1, "\pi") returns "-\pi" |
| 229 | # VS 8/1/2000 - slight adaption of code from T. Shemanske of Dartmouth College |
229 | # VS 8/1/2000 - slight adaption of code from T. Shemanske of Dartmouth College |
| 230 | sub preformat { |
230 | sub preformat { |
| 231 | my $num = shift; |
231 | my $num = shift; |
| 232 | my $obj = shift; |
232 | my $obj = shift; |
| 233 | my $out; |
233 | my $out; |
| 234 | |
234 | |
| 235 | |
235 | |
| 236 | if ($num == 0) { return 0; } |
236 | if ($num == 0) { return 0; } |
| 237 | elsif ($num == 1) { return $obj; } |
237 | elsif ($num == 1) { return $obj; } |
| 238 | elsif ($num == -1) { return "-".$obj; } |
238 | elsif ($num == -1) { return "-".$obj; } |
| 239 | |
239 | |
| 240 | return $num.$obj; |
240 | return $num.$obj; |
| … | |
… | |
| 244 | #factorial |
244 | #factorial |
| 245 | |
245 | |
| 246 | sub fact { |
246 | sub fact { |
| 247 | my $num = shift; |
247 | my $num = shift; |
| 248 | my $prod = 1; |
248 | my $prod = 1; |
| 249 | if ($num != int($num) or $num < 0) { |
249 | if ($num != int($num) or $num < 0) { |
| 250 | warn 'Non-negative integer required.'; |
250 | warn 'Non-negative integer required.'; |
| 251 | return; |
251 | return; |
| 252 | } |
252 | } |
| 253 | |
253 | |
| 254 | for (1..$num) { $prod *= $_ } |
254 | for (1..$num) { $prod *= $_ } |
| 255 | |
255 | |
| 256 | return $prod; |
256 | return $prod; |
| 257 | } |
257 | } |
| 258 | # return 1 so that this file can be included with require |
258 | # return 1 so that this file can be included with require |
| 259 | 1 |
259 | 1 |