Parent Directory
|
Revision Log
entered Mark's bug fix for floor()
1 #!/usr/local/bin/webwork-perl 2 sub _PGauxiliaryFunctions_init { 3 4 } 5 6 sub tan { 7 sin($_[0])/cos($_[0]); 8 } 9 sub cot { 10 cos($_[0])/sin($_[0]); 11 } 12 sub sec { 13 1/cos($_[0]); 14 } 15 sub csc { 16 1/sin($_[0]); 17 } 18 sub ln { 19 log($_[0]); 20 } 21 sub logten { 22 log($_[0])/log(10); 23 } 24 sub arcsin { 25 atan2 ($_[0],sqrt(1-$_[0]*$_[0])); 26 } 27 sub asin { 28 atan2 ($_[0],sqrt(1-$_[0]*$_[0])); 29 } 30 sub arccos { 31 atan2 (sqrt(1-$_[0]*$_[0]),$_[0]); 32 } 33 sub acos { 34 atan2 (sqrt(1-$_[0]*$_[0]),$_[0]); 35 } 36 sub arctan { 37 atan2($_[0],1); 38 } 39 sub atan { 40 atan2($_[0],1); 41 } 42 sub arccot { 43 atan2(1,$_[0]); 44 } 45 sub acot { 46 atan2(1,$_[0]); 47 } 48 sub sinh { 49 (exp($_[0]) - exp(-$_[0]))/2; 50 } 51 sub cosh { 52 (exp($_[0]) + exp(-$_[0]))/2; 53 } 54 sub tanh { 55 (exp($_[0]) - exp(-$_[0]))/(exp($_[0]) + exp(-$_[0])); 56 } 57 sub sech { 58 2/(exp($_[0]) + exp(-$_[0])); 59 } 60 sub sgn { 61 my $x = shift; 62 my $out; 63 $out = 1 if $x > 0; 64 $out = 0 if $x == 0; 65 $out = -1 if $x<0; 66 $out; 67 } 68 sub step { # heavyside function (1 or x>0) 69 my $x = shift; 70 ($x > 0 ) ? 1 : 0; 71 } 72 sub ceil { 73 my $x = shift; 74 - floor(-$x); 75 } 76 sub floor { 77 my $input = shift; 78 my $out = int $input; 79 $out -- if ( $out <= 0 and ($out-$input) > 0 ); # does the right thing for negative numbers 80 $out; 81 } 82 83 sub max { 84 85 my $maxVal = shift; 86 my @input = @_; 87 88 foreach my $num (@input) { 89 $maxVal = $num if ($maxVal < $num); 90 } 91 92 $maxVal; 93 94 } 95 96 sub min { 97 98 my $minVal = shift; 99 my @input = @_; 100 101 foreach my $num (@input) { 102 $minVal = $num if ($minVal > $num); 103 } 104 105 $minVal; 106 107 } 108 109 #round added 6/12/2000 by David Etlinger 110 sub round { 111 my $input = shift; 112 my $out; 113 if( $input >= 0 ) { 114 $out = int ($input + .5); 115 } 116 else { 117 $out = ceil($input - .5); 118 } 119 $out; 120 } 121 122 #least common multiple 123 #VS 6/29/2000 124 sub lcm { 125 my $a = shift; 126 my $b = shift; 127 128 #reorder such that $a is the smaller number 129 if ($a > $b) { 130 my $temp = $a; 131 $a = $b; 132 $b = $temp; 133 } 134 135 my $lcm = 0; 136 my $curr = $b;; 137 138 while($lcm == 0) { 139 $lcm = $curr if ($curr % $a == 0); 140 $curr += $b; 141 } 142 143 $lcm; 144 145 } 146 147 148 # greatest common factor 149 # takes in two scalar values and uses the Euclidean Algorithm to return the gcf 150 #VS 6/29/2000 151 152 sub gcf { 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 155 156 # reorder such that b is the smaller number 157 if ($a < $b) { 158 my $temp = $a; 159 $a = $b; 160 $b = $temp; 161 } 162 163 return $a if $b == 0; 164 165 my $q = int($a/$b); # quotient 166 my $r = $a % $b; # remainder 167 168 return $b if $r == 0; 169 170 my $tempR = $r; 171 172 while ($r != 0) { 173 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 176 $tempR = $r; 177 178 $a = $b; 179 $b = $r; 180 $q = $a/$b; 181 $r = $a % $b; 182 183 } 184 185 $tempR; 186 } 187 188 189 #greatest common factor. 190 #same as gcf, but both names are sufficiently common names 191 sub gcd { 192 return gcf($_[0], $_[1]); 193 } 194 195 #returns 1 for a prime number, else 0 196 #VS 6/30/2000 197 sub isPrime { 198 my $num = shift; 199 return 1 if ($num == 2 or $num == 3); 200 return 0 if ($num == 1 or $num == 0); 201 for (my $i = 3; $i <= $num/2; $i++) { return 0 if ($num % $i == 0); } 202 return 1; 203 } 204 205 #reduces a fraction, returning an array containing ($numerator, $denominator) 206 #VS 7/10/2000 207 sub reduce { 208 209 my $num = shift; 210 my $denom = shift; 211 my $gcd = gcd($num, $denom); 212 213 $num = $num/$gcd; 214 $denom = $denom/$gcd; 215 216 # formats such that only the numerator will be negative 217 if ($num/$denom < 0) {$num = -abs($num); $denom = abs($denom);} 218 else {$num = abs($num); $denom = abs($denom);} 219 220 my @frac = ($num, $denom); 221 @frac; 222 } 223 224 225 # takes a number and fixed object, as in "$a x" and formats 226 # to account for when $a = 0, 1, -1 227 # Usage: format($scalar, "quoted string"); 228 # Example: format(-1, "\pi") returns "-\pi" 229 # VS 8/1/2000 - slight adaption of code from T. Shemanske of Dartmouth College 230 sub preformat { 231 my $num = shift; 232 my $obj = shift; 233 my $out; 234 235 236 if ($num == 0) { return 0; } 237 elsif ($num == 1) { return $obj; } 238 elsif ($num == -1) { return "-".$obj; } 239 240 return $num.$obj; 241 } 242 243 244 #factorial 245 246 sub fact { 247 my $num = shift; 248 my $prod = 1; 249 if ($num != int($num) or $num < 0) { 250 warn 'Non-negative integer required.'; 251 return; 252 } 253 254 for (1..$num) { $prod *= $_ } 255 256 return $prod; 257 } 258 # return 1 so that this file can be included with require 259 1
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |