1=head1 NAME 2 3Math::NumberCruncher - Collection of useful math-related functions. 4 5=head1 SYNOPSIS 6 7It should be noted that as of v4.0, there is now an OO interface to Math::NumberCruncher. For backwards compatibility, however, the previous, functional style will always be supported. 8 9# OO Style 10 11use Math::NumberCruncher; 12 13$ref = Math::NumberCruncher->new(); 14 15# From this point on, all of the subroutines shown below will be available 16# through $ref (i.e., ( $high,$low ) = $ref->Range( \@array )). For the sake 17# of brevity, consult the functional documentation (below) for the use 18# of specific functions. 19 20# Functional Style 21 22use Math::NumberCruncher; 23 24($high, $low) = Math::NumberCruncher::Range(\@array); 25 26$mean = Math::NumberCruncher::Mean(\@array); 27 28$median = Math::NumberCruncher::Median(\@array [, $decimal_places]); 29 30$odd_median = Math::NumberCruncher::OddMedian(\@array); 31 32$mode = Math::NumberCruncher::Mode(\@array); 33 34$covariance = Math::NumberCruncher::Covariance(\@array1, \@array2); 35 36$correlation = Math::NumberCruncher::Correlation(\@array1, \@array2); 37 38($slope, $y_intercept) = Math::NumberCruncher::BestFit(\@array1, \@array2 [, $decimal_places]); 39 40$distance = Math::NumberCruncher::Distance($x1,$y1,$z1,$x2,$y2,$z2 [, $decimal_places]); 41 42$distance = Math::NumberCruncher::Distance($x1,$y1,$x1,$x2 [, $decimal_places]); 43 44$distance = Math::NumberCruncher::ManhattanDistance($x1,$y1,$x2,$y2); 45 46$probAll = Math::NumberCruncher::AllOf('0.3','0.25','0.91','0.002'); 47 48$probNone = Math::NumberCruncher::NoneOf('0.4','0.5772','0.212'); 49 50$probSome = Math::NumberCruncher::SomeOf('0.11','0.56','0.3275'); 51 52$factorial = Math::NumberCruncher::Factorial($some_number); 53 54$permutations = Math::NumberCruncher::Permutation($n); 55 56$permutations = Math::NumberCruncher::Permutation($n,$k); 57 58$roll = Math::NumberCruncher::Dice(3,12,4); 59 60$randInt = Math::NumberCruncher::RandInt(10,50); 61 62$randomElement = Math::NumberCruncher::RandomElement(\@array); 63 64Math::NumberCruncher::ShuffleArray(\@array); 65 66@unique = Math::NumberCruncher::Unique(\@array); 67 68@a_only = Math::NumberCruncher::Compare(\@a,\@b); 69 70@union = Math::NumberCruncher::Union(\@a,\@b); 71 72@intersection = Math::NumberCruncher::Intersection(\@a,\@b); 73 74@difference = Math::NumberCruncher::Difference(\@a,\@b); 75 76$gaussianRand = Math::NumberCruncher::GaussianRand(); 77 78$ways = Math::NumberCruncher::Choose($n,$k); 79 80$binomial = Math::NumberCruncher::Binomial($attempts,$successes,$probability); 81 82$gaussianDist = Math::NumberCruncher::GaussianDist($x,$mean,$variance); 83 84$StdDev = Math::NumberCruncher::StandardDeviation(\@array [, $decimal_places]); 85 86$variance = Math::NumberCruncher::Variance(\@array [, $decimal_places]); 87 88@scores = Math::NumberCruncher::StandardScores(\@array [, $decimal_places]); 89 90$confidence = Math::NumberCruncher::SignSignificance($trials,$hits,$probability); 91 92$e = Math::Numbercruncher::EMC2( "m512", "miles" [, $decimal_places] ); 93 94$m = Math::NumberCruncher::EMC2( "e987432" "km" [, $decimal_places] ); 95 96$force = Math::NumberCruncher::FMA( "m12", "a73.5" [, $decimal_places] ); 97 98$mass = Math::NumberCruncher::FMA( "a43", "f1324" [, $decimal_places] ); 99 100$acceleration = Math::NumberCruncher::FMA( "f53512", "m356" [, $decimal_places] ); 101 102$predicted_value = Math::NubmerCruncher::Predict( $slope, $y_intercept, $proposed_x [, $decimal_places] ); 103 104$area = Math::NumberCruncher::TriangleHeron( $a, $b, $c [, $decimal_places] ); 105 106$area = Math::NumberCruncher::TriangleHeron( 1,3, 5,7, 8,2 [, $decimal_places] ); 107 108$perimeter = Math::NumberCruncher::PolygonPerimeter( $x0,$y0, $x1,$y1, $x2,$y2, ... [, p$decimal_places]); 109 110$direction = Math::NumberCruncher::Clockwise( $x0,$y0, $x1,$y1, $x2,$y2 ); 111 112$collision = Math::NumberCruncher::InPolygon( $x, $y, @xy ); 113 114@points = Math::NumberCruncher::BoundingBox_Points( $d, @p ); 115 116$in_triangle = Math::NumberCruncher::InTriangle( $x,$y, $x0,$y0, $x1,$y1, $x2,$y2 ); 117 118$area = Math::NumberCruncher::PolygonArea( 0, 1, 1, 0, 2, 0, 3, 2, 2, 3 [, p$decimal_places] ); 119 120$area = Math::NumberCruncher::CircleArea( $diameter [, $decimal_places] ); 121 122$circumference = Math::NumberCruncher::Circumference( $diameter [, $decimal_places] ); 123 124$volume = Math::NumberCruncher::SphereVolume( $radius [, $decimal_places] ); 125 126$surface_area = Math::NumberCruncher::SphereSurface( $radius [, $decimal_places] ); 127 128$years = Math::NumberCruncher::RuleOf72( $interest_rate [, $decimal_places] ); 129 130$volume = Math::NumberCruncher::CylinderVolume( $radius, $height [, $decimal_places] ); 131 132$volume = Math::NumberCruncher::ConeVolume( $lowerBaseArea, $height [, $decimal_places] ); 133 134$radians = Math::NumberCruncher::deg2rad( $degrees [, $decimal_places] ); 135 136$degrees = Math::NumberCruncher::rad2deg( $radians [, $decimal_places] ); 137 138$Fahrenheit = Math::NumberCruncher::C2F( $Celsius [, $decimal_places] ); 139 140$Celsius = Math::NumberCruncher::F2C( $Fahrenheit [, $decimal_places] ); 141 142$cm = Math::NumberCruncher::in2cm( $inches [, $decimal_places] ); 143 144$inches = Math::NumberCruncher::cm2in( $cm [, $decimal_places] ); 145 146$ft = Math::NumberCruncher::m2ft( $m [, $decimal_places] ); 147 148$m = Math::NumberCruncher::ft2m( $ft [, $decimal_places] ); 149 150$miles = Math::NumberCruncher::km2miles( $km [, $decimal_places] ); 151 152$km = Math::NumberCruncher::miles2km( $miles [, $decimal_places] ); 153 154$lb = Math::NumberCruncher::kg2lb( $kg [, $decimal_places] ); 155 156$kg = Math::NumberCruncher::lb2kg( $lb [, $decimal_places] ); 157 158$RelativeStride = Math::NumberCruncher::RelativeStride( $stride_length, $leg_length [, $decimal_places] ); 159 160$RelativeStride = Math::NumberCruncher::RelativeStride_2( $DimensionlessSpeed [, $decimal_places] ); 161 162$DimensionlessSpeed = Math::NumberCruncher::DimensionlessSpeed( $RelativeStride [, $decimal_places] ); 163 164$DimensionlessSpeed = Math::NumberCruncher::DimensionlessSpeed_2( $ActualSpeed, $leg_length [, $decimal_places]); 165 166$ActualSpeed = Math::NumberCruncher::ActualSpeed( $leg_length, $DimensionlessSpeed [, $decimal_places] ); 167 168$eccentricity = Math::NumberCruncher::Eccentricity( $half_major_axis, $half_minor_axis [, $decimal_places] ); 169 170$LatusRectum = Math::NumberCruncher::LatusRectum( $half_major_axis, $half_minor_axis [, $decimal_places] ); 171 172$EllipseArea = Math::NumberCruncher::EllipseArea( $half_major_axis, $half_minor_axis [, $decimal_places] ); 173 174$OrbitalVelocity = Math::NumberCruncher::OrbitalVelocity( $r, $a, $M [, $decimal_places] ); 175 176$sine = Math::NumberCruncher::sin( $x [, $decimal_places] ); 177 178$cosine = Math::NumberCruncher::cos( $x [, $decimal_places] ); 179 180$tangent = Math::NumberCruncher::tan( $x [, $decimal_places] ); 181 182$arcsin = Math::NumberCruncher::asin( $x [, $decimal_places] ); 183 184$arccos = Math::NumberCruncher::acos( $x [, $decimal_places] ); 185 186$arctan = Math::NumberCruncher::atan( $x [, $decimal_places] ); 187 188$cotangent = Math::NumberCruncher::cot( $x [, $decimal_places] ); 189 190$arccot = Math::NumberCruncher::acot( $x [, $decimal_places] ); 191 192$secant = Math::NumberCruncher::sec( $x [, $decimal_places] ); 193 194$arcsec = Math::NumberCruncher::asec( $x [, $decimal_places] ); 195 196$cosecant = Math::NumberCruncher::csc( $x [, $decimal_places] ); 197 198$arccosecant = Math::NumberCruncher::acsc( $x [, $decimal_places] ); 199 200$exsecant = Math::NumberCruncher::exsec( $x [, $decimal_places] ); 201 202$versine = Math::NumberCruncher::vers( $x [, $decimal_places] ); 203 204$coversine = Math::NumberCruncher::covers( $x [, $decimal_places] ); 205 206$haversine = Math::NumberCruncher::hav( $x [, $decimal_places] ); 207 208$grouped = Math::NumberCruncher::Commas( $number ); 209 210$SqrRoot = Math::NumberCruncher::SqrRoot( $number [, $decimal_places] ); 211 212$square_root = Math::NumberCruncher::sqrt( $x [, $decimal_places] ); 213 214$root = Math::NumberCruncher::Root( 55, 3 [, $decimal_places] ); 215 216$root = Math::NumberCruncher::Root2( 55, 3 [, $decimal_places] ); 217 218$log = Math::NumberCruncher::Ln( 100 [, $decimal_places] ); 219 220$log = Math::NumberCruncher::log( $num [, $decimal_places] ); 221 222$num = Math::NumberCruncher::Exp( 0.111 [, $decimal_places] ); 223 224$num = Math::NumberCruncher::exp( $log [, $decimal_places] ); 225 226$Pi = Math::NumberCruncher::PICONST( $decimal_places ); 227 228$E = Math::NumberCruncher::ECONST( $decimal_places ); 229 230( $A, $B, $C ) = Math::NumberCruncher::PythagTriples( $x, $y [, $decimal_places] ); 231 232$z = Math::NumberCruncher::PythagTriplesSeq( $x, $y [, $decimal_places] ); 233 234@nums = Math::NumberCruncher::SIS( [$start, $numbers, $increment] ); 235 236$inverse = Math::NumberCruncher::Inverse( $number [, $decimal_places] ); 237 238@constants = Math::NumberCruncher::CONSTANTS( 'all' [, $decimal_places] ); 239 240$bernoulli = Math::NumberCruncher::Bernoulli( $num [, $decimal_places] ); 241 242@bernoulli = Math::NumberCruncher::Bernoulli( $num ); 243 244=head1 DESCRIPTION 245 246This module is a collection of commonly needed number-related functions, including numerous standard statistical, 247geometric, and probability functions. Some of these functions are taken directly from _Mastering Algorithms with Perl_, 248by Jon Orwant, Jarkko Hietaniemi, and John Macdonald, and others are adapted heavily from same. The remainder are 249either original functions written by the author, or original adaptations of standard algorithms. Some of the functions 250are fairly obvious, others are explained in greater detail below. For all calculations involving pi, the value of pi is 251taken out to 2000 places. Overkill? Probably, but it is better, in my opinion, to have too much accuracy as opposed to 252not enough. I've also included the value of Euler's e, g (Newton's gravitational constant), and the natural log of 2 253out to 2000 places. These are available for export as $PI, $_e_, $_g_, and $_ln2_, respectively. In addition, via the 254CONSTANT() routine, the Golden Mean, Catalan constant, Apery constant, Landau-Ramanujan constant, Khintchine constant, 255Sierpinski constant, Wilbraham-Gibbs constant, Euler's gamma, square root of 2, square root of 3, and square root of 5 256are pre-calculated to 2000 decimal places and are constructed only as requested by the user. See below for further 257details. Additionally, sqrt, sin, cos, log, and exp are suitable as drop-in replacements for the built-in functions of 258the same name. Usage is exactly the same, the only difference being the number of default decimal places is 20, and 259can be changed on the fly with each call. Further details below. 260 261The default number of decimal places throughout is 20. This can be modified either by changing the value of $DECIMALS 262at the top of the NumberCruncher.pm file itself, or it can be changed for the duration of a given script by modifying 263$Math::NumberCruncher::DECIMALS. Or, where noted, you can specify a number of decimal places on a given call. For 264example, if you want the square root of two taken out to the default 20 decimal places, you can simply use: "$root = 265Math::NumberCruncher::SqrRoot( 2 )". However, if you want to take the square root of two out to, say, 100 decimal 266places, you can use: "$root = Math::NumberCruncher::SqrRoot( 2, 100 )". 267 268The following functions are available for export: sqrt, sin, asin, cos, acos, tan, atan, cot, acot, sec, asec, csc, 269acsc, vers, covers, hav, log, exp. Where there is a function of the same name as a built-in function, the 270Math::NumberCruncher version is suitable as a drop-in replacement, allowing for a greater degree of accuracy. It should 271be noted, however, that the functions are potentially a good deal slower than the built-in functions, depending upon 272the complexity of the call. For a simple call, like the square root of 1111 taken out to 50 decimal places, the result 273is reasonably fast. For something like the 20th root of 123456789.9876543210000001, expect it to take substantially 274longer. 275 276=head1 EXAMPLES 277 278=head2 ($high,$low) = B<Math::NumberCruncher::Range>(\@array); 279 280Returns the largest and smallest elements in an array. 281 282=head2 $mean = B<Math::NumberCruncher::Mean>(\@array); 283 284Returns the mean, or average, of an array. 285 286=head2 $median = B<Math::NumberCruncher::Median>(\@array [, $decimal_places]); 287 288Returns the median, or the middle, of an array. The median may or may not be an element of the array itself. 289 290=head2 $odd_median = B<Math::NumberCruncher::OddMedian>(\@array); 291 292Returns the odd median, which, unlike the median, *is* an element of the array. In all other respects it is similar to the median. 293 294=head2 $mode = B<Math::NumberCruncher::Mode>(\@array); 295 296Returns the mode, or most frequently occurring item, of @array. 297 298=head2 $covariance = B<Math::NumberCruncher::Covariance>(\@array1,\@array2); 299 300Returns the covariance, which is a measurement of the correlation of two variables. 301 302=head2 $correlation = B<Math::NumberCruncher::Correlation>(\@array1,\@array2); 303 304Returns the correlation of two variables. Correlation ranges from 1 to -1, with a correlation of zero meaning no correlation exists between the two variables. 305 306=head2 ($slope,$y_intercept ) = B<Math::NumberCruncher::BestFit>(\@array1,\@array2 [, $decimal_places]); 307 308Returns the slope and y-intercept of the line of best fit for the data in question. 309 310=head2 $distance = B<Math::NumberCruncher::Distance>($x1,$y1,$x1,$x2 [, $decimal_places]); 311 312Returns the Euclidian distance between two points. The above example demonstrates the use in two dimensions. For three dimensions, usage would be $distance = B<Math::NumberCruncher::Distance>($x1,$y1,$z1,$x2,$y2,$z2);> 313 314=head2 $distance = B<Math::NumberCruncher::ManhattanDistance>($x1,$y1,$x2,$y2); 315 316Modified two-dimensional distance between two points. As stated in _Mastering Algorithms with Perl_, "Helicopter pilots tend to think in Euclidian distance, good New York cabbies tend to think in Manhattan distance." Rather than distance "as the crow flies," this is distance based on a rigid grid, or network of streets, like those found in Manhattan. 317 318=head2 $probAll = B<Math::NumberCruncher::AllOf>('0.3','0.25','0.91','0.002'); 319 320The probability that B<all> of the probabilities in question will be satisfied. (i.e., the probability that the Steelers will win the SuperBowl B<and> that David Tua will win the World Heavyweight Title in boxing.) 321 322=head2 $probNone = B<Math::NumberCruncher::NoneOf>('0.4','0.5772','0.212'); 323 324The probability that B<none> of the probabilities in question will be satisfied. (i.e., the probability that the Steelers will not win the SuperBowl and that David Tua will not win the World Heavyweight Title in boxing.) 325 326=head2 $probSome = B<Math::NumberCruncher::SomeOf>('0.11','0.56','0.3275'); 327 328The probability that at least one of the probabilities in question will be satisfied. (i.e., the probability that either the Steelers will win the SuperBowl B<or> David Tua will win the World Heavyweight Title in boxing.) 329 330=head2 $factorial = B<Math::NumberCruncher::Factorial>($some_number); 331 332The number of possible orderings of $factorial items. The factorial n! gives the number of ways in which n objects can be permuted. 333 334=head2 $permutations = B<Math::NumberCruncher::Permutation>($n); 335 336The number of permutations of $n elements. 337 338=head2 $permutations = B<Math::NumberCruncher::Permutation>($n,$k); 339 340The number of permutations of $k elements drawn from a set of $n elements. 341 342=head2 $roll = B<Math::NumberCruncher::Dice>($number,$sides,$plus); 343 344The obligatory dice rolling routine. Returns the result after passing the number of rolls of the die, the number of sides of the die, and any additional points to be added to the roll. As commonly seen in role playing games, 4d12+5 would be expressed as B<Dice(4,12,5)>. The function defaults to a single 6-sided die rolled once without any points added. 345 346=head2 $randInt = B<Math::NumberCruncher::RandInt>(10,50); 347 348Returns a random integer between the two number passed to the function, inclusive. With no parameters passed, the function returns either 0 or 1. 349 350=head2 $randomElement = B<Math::NumberCruncher::RandomElement>(\@array); 351 352Returns a random element from @array. 353 354=head2 B<Math::NumberCruncher::ShuffleArray>(\@array); 355 356Shuffles the elements of @array and returns them. 357 358=head2 @unique = B<Math::NumberCruncher::Unique>(\@array); 359 360Returns an array of the unique items in an array. 361 362=head2 @a_only = B<Math::NumberCruncher::Compare>(\@a,\@b); 363 364Returns an array of elements that appear only in the first array passed. Any elements that appear in both arrays, or appear only in the second array, are discarded. 365 366=head2 @union = B<Math::NumberCruncher::Union>(\@a,\@b); 367 368Returns an array of the unique elements produced from the joining of the two arrays. 369 370=head2 @intersection = B<Math::NumberCruncher::Intersection>(\@a,\@b); 371 372Returns an array of the elements that appear in both arrays. 373 374=head2 @difference = B<Math::NumberCruncher::Difference>(\@a,\@b); 375 376Returns an array of the symmetric difference of the two arrays. For example, in the words of _Mastering Algorithms in Perl_, "show me the web documents that talk about Perl B<or> about sets B<but not> those that talk about B<both>. 377 378=head2 $gaussianRand = B<Math::NumberCruncher::GaussianRand>(); 379 380Returns one or two floating point numbers based on the Gaussian Distribution, based upon whether the call wants an array or a scalar value. 381 382=head2 $ways = B<Math::NumberCruncher::Choose>($n,$k); 383 384The number of ways to choose $k elements from a set of $n elements, when the order of selection is irrelevant. 385 386=head2 $binomial = B<Math::NumberCruncher::Binomial>($n,$k,$p); 387 388Returns the probability of $k successes in $n tries, given a probability of $p. (i.e., if the probability of being struck by lightning is 1 in 75,000, in 100 days, the probability of being struck by lightning exactly twice would be expressed as B<Binomial('100','2','0.0000133')>) 389 390=head2 $probability = B<Math::NumberCruncher::GaussianDist>($x,$mean,$variance); 391 392Returns the probability, based on Gaussian Distribution, of our random variable, $x, given the $mean and $variance. 393 394=head2 $StdDev = B<Math::NumberCruncher::StandardDeviation>(\@array [, $decimal_places]); 395 396Returns the Standard Deviation of @array, which is a measurement of how diverse your data is. 397 398=head2 $variance = B<Math::NumberCruncher::Variance>(\@array [, $decimal_places]); 399 400Returns the variance for @array, which is the square of the standard deviation. Or think of standard deviation as the square root of the variance. Variance is another indicator of the diversity of your data. 401 402=head2 @scores = B<Math::NumberCruncher::StandardScores>(\@array [, $decimal_places]); 403 404Returns an array of the number of standard deviations above the mean for @array. 405 406=head2 $confidence = B<Math::NumberCruncher::SignSignificance>($trials,$hits,$probability); 407 408Returns the probability of how likely it is that your data is due to chance. The lower the confidence, the less likely your data is due to chance. 409 410=head2 $e = B<Math::NumberCruncher::EMC2>( "m36", "km" [, $decimal_places] ); 411 412Implementation of Einstein's E=MC**2. Given either energy or mass, the function returns the other. When passing mass, the value must be preceeded by a "m," which may be either upper or lower case. When passing energy, the value must be preceeded by a "e," which may be either upper or lower case. The second argument is whether you wish to use kilometers per second or miles per second for the speed of light. Case is irrelevant. EMC2() keys off of the first letter of the second argument, so all that is necessary to pass is either "k" or "m". 413 414=head2 $force = B<Math::NumberCruncher::FMA>( "m97", "a53" [, $decimal_places] ); 415 416Implementation of the stadard force = mass * acceleration formula. Given two of the three variables (i.e., mass and force, mass and acceleration, or acceleration and force), the function returns the third. When passing the values, mass must be preceeded by a "m," force must be preceeded by a "f," and acceleration must be preceeded by an "a." Case is irrelevant. 417 418=head2 $predicted = B<Math::NumberCruncher::Predict>( $slope, $y_intercept, $proposed_x [, $decimal_places] ); 419 420Useful for predicting values based on data trends, as calculated by BestFit(). Given the slope and y-intercept, and a proposed value of x, returns corresponding y. 421 422=head2 $area = B<Math::NumberCruncher::TriangleHeron>( $a, $b, $c [, $decimal_places] ); 423 424Calculates the area of a triangle, using Heron's formula. TriangleHeron() can be passed either the lengths of the three sides of the triangle, or the (x,y) coordinates of the three verticies. 425 426=head2 $perimeter = B<Math::NumberCruncher::PolygonPerimeter>( $x0,$y0, $x1,$y1, $x2,$y2, ... [, p$decimal_places]); 427 428Calculates the length of the perimeter of a given polygon. The final argument specifies the number of decimal places you want. To specify a number other than the default (see above), the number must be preceeded by the letter "p". For example: Math::NumberCruncher::PolygonPerimeter( 1, 1, 2, 3, 4, 5, p75 ); 429 430=head2 $direction = B<Math::NumberCruncher::Clockwise>( $x0,$y0, $x1,$y1, $x2,$y2 ); 431 432Given three pairs of points, returns a positive number if you must turn clockwise when moving from p1 to p2 to p3, returns a negative number if you must turn counter-clockwise when moving from p1 to p2 to p3, and a zero if the three points lie on the same line. 433 434=head2 $collision = B<Math::NumberCruncher::InPolygon>( $x, $y, @xy ); 435 436Given a set of xy pairs (@xy) that define the perimeter of a polygon, returns a 1 if point ($x,$y) is inside the polygon and returns 0 if the point ($x,$y) is outside the polygon. 437 438=head2 @points = B<Math::NumberCruncher::BoundingBox_Points>( $d, @p ); 439 440Given a set of @p points and $d dimensions, returns two points that define the upper left and lower right corners of the bounding box for set of points @p. 441 442=head2 $in_triangle = B<Math::NumberCruncher::InTriangle>( $x,$y, $x0,$y0, $x1,$y1, $x2,$y2 ); 443 444Returns true if point $x,$y is inside the triangle defined by points ($x0,$y0), ($x1,$y1), and ($x2,$y2) 445 446=head2 $area = B<Math::NumberCruncher::PolygonArea>( 0, 1, 1, 0, 3, 2, 2, 3, 0, 2 [, p$decimal_places]); 447 448Calculates the area of a polygon using determinants. As with PolygonPerimeter(), the final argument specified the number of decimal places you want. See PolygonPerimeter(), above, for details. 449 450=head2 $area = B<Math::NumberCruncher::CircleArea>( $diameter [, $decimal_places] ); 451 452Calculates the area of a circle, given the diameter. 453 454=head2 $circumference = B<Math::NumberCruncher::Circumference>( $diameter [, $decimal_places] ); 455 456Calculates the circumference of a circle, given the diameter. 457 458=head2 $volume = B<Math::NumberCruncher::SphereVolume>( $radius [, $decimal_places] ); 459 460Calculates the volume of a sphere, given the radius. 461 462=head2 $surface_area = B<Math::NumberCruncher::SphereSurface>( $radius [, $decimal_places] ); 463 464Calculates the surface area of a sphere, given the radius. 465 466=head2 $years = B<Math::NumberCruncher::RuleOf72>( $interest_rate [, $decimal_places] ); 467 468A very simple financial formula. It calculates how many years, at a given interest rate, it will take to double your money, provided that the money and all interest is left in the account. 469 470=head2 $volume = B<Math::NumberCruncher::CylinderVolume>( $radius, $height [, $decimal_places] ); 471 472Calculates the volume of a cylinder given the radius and the height. 473 474=head2 $volume = B<Math::NumberCruncher::ConeVolume>( $lowerBaseArea, $height [, $decimal_places] ); 475 476Calculates the volume of a cone given the lower base area and the height. 477 478=head2 $radians = B<Math::NumberCruncher::deg2rad>( $degrees [, $decimal_places] ); 479 480Converts degrees to radians. 481 482=head2 $degrees = B<Math::NumberCruncher::rad2deg>( $radians [, $decimal_places] ); 483 484Converts radians to degrees. 485 486=head2 $Fahrenheit = B<Math::NumberCruncher::C2F>( $Celsius [, $decimal_places] ); 487 488Converts Celsius to Fahrenheit. 489 490=head2 $Celsius = B<Math::NumberCruncher::F2C>( $Fahrenheit [, $decimal_places] ); 491 492Converts Fahrenheit to Celsius. 493 494=head2 $cm = B<Math::NumberCruncher::in2cm>( $inches [, $decimal_places] ); 495 496Converts inches to centimeters. 497 498=head2 $inches = B<Math::NumberCruncher::cm2in>( $cm [, $decimal_places] ); 499 500Converts centimeters to inches. 501 502=head2 $ft = B<Math::NumberCruncher::m2ft>( $m [, $decimal_places] ); 503 504Converts meters to feet. 505 506=head2 $m = B<Math::NumberCruncher::ft2m>( $ft [, $decimal_places] ); 507 508Converts feet to meters. 509 510=head2 $miles = B<Math::NumberCruncher::km2miles>( $km [, $decimal_places] ); 511 512Converts kilometers to miles. 513 514=head2 $km = B<Math::NumberCruncher::miles2km>( $miles [, $decimal_places] ); 515 516Converst miles to kilometers. 517 518=head2 $lb = B<Math::NumberCruncher::kg2lb>( $kg [, $decimal_places] ); 519 520Converts kilograms to pounds. 521 522=head2 $kg = B<Math::NumberCruncher::lb2kg>( $lb [, $decimal_places] ); 523 524Converts pounds to kilograms. 525 526=head2 $RelativeStride = B<Math::NumberCruncher::RelativeStride>( $stride_length, $leg_length [, $decimal_places] ); 527 528Welcome to the world of ichnology. This was originally for a dinosaur simulation I have been working on. This and the following four routines are all part of determining the speed of a dinosaur (or any other animal, including people), based on leg measurements and stride measurements. Ichnology is study of trace fossils (i.e., nests, eggs, fossilized dung...seriously, that's not a joke), and in this case, fossilized footprints, or trackways. RelativeStride() is for determining the relative stride of the animal given stride length and leg length. 529 530=head2 $RelativeStride = B<Math::NumberCruncher::RelativeStride_2>( $DimensionlessSpeed [, $decimal_places] ); 531 532This differs from the previous routine in that it calculates relative stride based on dimensionless speed, rather than stride and leg length. 533 534=head2 $DimensionlessSpeed = B<Math::NumberCruncher::DimensionlessSpeed>( $RelativeStride [, $decimal_places] ); 535 536Dimensionless speed is a calculated value that relates the speed of an animal to leg length and stride length. 537 538=head2 $DimensionlessSpeed = B<Math::NumberCruncher::DimensionlessSpeed_2>( $speed, $legLength [, $decimal_places] ); 539 540This differs from the previous routine in that it calculates dimensionless speed based on actual speed and leg length. 541 542=head2 $ActualSpeed = B<Math::NumberCruncher::ActualSpeed>( $leg_length, $DimensionlessSpeed [, $decimal_places] ); 543 544This is the really interesting one. Given leg length and dimensionless speed, it returns the actual speed (or absolute speed) of the animal in question in distance per second. There is no unit of measure conversion performed, so if you pass it measurements in meters, the answer is in meters per second. If you pass it measurements in inches, it returns inches per second, and so on. 545 546=head2 $eccentricity = B<Math::NumberCruncher::Eccentricity>( $half_major_axis, $half_minor_axis [, $decimal_places] ); 547 548Calculates the eccentricity of an ellipse, given the semi-major axis and the semi-minor axis. 549 550=head2 $LatusRectum = B<Math::NumberCruncher::LatusRectum>( $half_major_axis, $half_minor_axis [, $decimal_places] ); 551 552Calculates the latus rectum of an ellipse, given the semi-major axis and the semi-minor axis. 553 554=head2 $EllipseArea = B<Math::NumberCruncher::EllipseArea>( $half_major_axis, $half_minor_axis [, $decimal_places] ); 555 556Calculates the area of an ellipse, given the semi-major axis and the semi-minor axis. 557 558=head2 $OrbitalVelocity = B<Math::NumberCruncher::OrbitalVelocity>( $r, $a, $M [, $decimal_places] ); 559 560Calculates orbital velocity of an object given the radial distance at a given point on an elliptical orbit, the mean distance of the central object, and the mass of the central object. 561 562=head2 $SqrRoot = B<Math::NumberCruncher::SqrRoot>( $number [, $decimal_places] ); 563 564Calculates the square root of a number out to an arbitrary number of decimal places. It should be noted that this method is potentially substantially slower than the built-in sqrt() function. However, especially with large numbers, this method is far more accurate. 565 566=head2 $sqrt = B<Math::NumberCruncher::sqrt>( $number [, $decimal_places] ); 567 568An alias for SqrRoot. This is exportable and is suitable as a drop-in replacement for the built-in sqrt() function. 569 570=head2 $root = B<Math::NumberCruncher::Root>( 55, 3 [, $decimal_places] ); 571 572Calculates the N-th root of a given number using Newton's Method. In the above example, $root is the cube root of 55. Root() tends to be faster than Root2() when dealing with integers, or numbers with few decimal places. 573 574=head2 $root = B<Math::NumberCruncher::Root2>( 55, 3 [, $decimal_places] ); 575 576Calculates the N=th root of a given number using logarithms. In the above example, $root is the cube root of 55. Root2() tends to be faster than Root() when dealing with numbers containing multiple decimal places. 577 578=head2 $sin = B<Math::NumberCruncher::sin>( $x [, $decimal_places] ); 579 580Calculates the sine. This is available for export and is suitable as a drop-in replacement for the built-in sin() function. 581 582=head2 $cos = B<Math::NumberCruncher::cos>( $x [, $decimal_places] ); 583 584Calculates the cosine. This is available for export and is suitable as a drop-in replacement for the built-in cos() function. 585 586=head2 $tan = B<Math::NumberCruncher::tan>( $x [, $decimal_places] ); 587 588Calculates the tangent. 589 590=head2 $arcsin = B<Math::NumberCruncher::asin>( $x [, $decimal_places] ); 591 592Calculates the inverse sine. 593 594=head2 $arccos = B<Math::NumberCruncher::acos>( $x [, $decimal_places] ); 595 596Calculates the inverse cosine. 597 598=head2 $arctan = B<Math::NumberCruncher::atan>( $x [, $decimal_places] ); 599 600Calculates the inverse tangent. 601 602=head2 $secant = B<Math::NumberCruncher::sec>( $x [, $decimal_places] ); 603 604Calculates the secant. 605 606=head2 $arcsec = B<Math::NumberCruncher::asec>( $x [, $decimal_places] ); 607 608Calculates the inverse secant. 609 610=head2 $cosecant = B<Math::NumberCruncher::csc>( $x [, $decimal_places] ); 611 612Calculates the cosecant. 613 614=head2 $arccosecant = B<Math::NumberCruncher::acsc>( $x [, $decimal_places] ); 615 616Calculates the inverse of the cosecant. 617 618=head2 $exsecant = B<Math::NumberCruncher::exsec>( $x [, $decimal_places] ); 619 620Calculates the exsecant. 621 622=head2 $cotangent = B<Math::NumberCruncher::cot>( $x [, $decimal_places] ); 623 624Calculates the cotangent. 625 626=head2 $arccot = B<Math::NumberCruncher::acot>( $x [, $decimal_places] ); 627 628Calculates the inverse cotangent. 629 630=head2 $versine = B<Math::NumberCruncher::vers>( $x [, $decimal_places] ); 631 632Calculates the versine. 633 634=head2 $coversine = B<Math::NumberCruncher::covers>( $x [, $decimal_places] ); 635 636Calculates the coversine. 637 638=head2 $haversine = B<Math::NumberCruncher::hav>( $x [, $decimal_places] ); 639 640Calculates the haversine. 641 642=head2 $grouped = B<Math::NumberCruncher::Commas>( $number ); 643 644Performs digit grouping, making large number more visually pleasing. 645 646=head2 $log = B<Math::NumberCruncher::Ln>( 100 [, $decimal_places] ); 647 648Calculates the natural log of a given number to a given number of decimal places. 649 650=head2 $log = B<Math::NumberCruncher::log>( $num [, $decimal_places] ); 651 652An alias for Log(). This is exportable and is suitable as a drop-in replacement for the built-in log() function. 653 654=head2 $num = B<Math::NumberCruncher::Exp>( $log [, $decimal_places] ); 655 656Performs the inverse of Ln(). 657 658=head2 $num = B<Math::NumberCruncher::exp>( $log [, $decimal_places] ); 659 660An alias for Exp(). This is exportable and is suitable as a drop-in replacement for the built-in exp() function. 661 662=head2 $Pi = B<Math::NumberCruncher::PICONST>( $decimal_places ); 663 664Calculates Pi out to an arbitrary number of decimal places. Math::NumberCruncher has Pi pre-calculated out to 2000 decimal places. If you want more decimal places than 2000, be advised that this can take a non-trivial length of time. 665 666=head2 $E = B<Math::NumberCruncher::ECONST>( $decimal_Places ); 667 668Calculaes Euler's number e out to an arbitrary number of decimal places. Math::NumberCruncher has e pre-calculated out to 2000 decimal places. If you want more decimal places than 2000, be advised that this can take a non-trivial length of time. 669 670=head2 ( $A, $B, $C ) = B<Math::NumberCruncher::PythagTriples>( 5, 7 [, $decimal_places] ); 671 672Calculates Pythagorian Triples based on the two numbers passed. Remember Pythagorian Triples are three numbers where the sum of the squares of the first two numbers is equal to the square of the third. 673 674=head2 $z = B<Math::NumberCruncher::PythagTriplesSeq>( 55, 32 [, $decimal_places] ); 675 676Completes the Pythagorian Triple sequence based on the two numbers passed. 677 678=head2 @nums = B<Math::NumberCruncher::SIS>( [$start, $numbers, $increment] ); 679 680Returns an array of numbers in a super-increasing sequence. All parameters are optional. You can pass the number with which you want the sequence to start, the quantity numbers you want returned, and by how much you want to increase the next number over the sum of all of the previous numbers. By default, start is 1, numbers returned is 50, and increment is 1. 681 682=head2 $inverse = B<Math::NumberCruncher::Inverse>( $num [, $decimal_places] ); 683 684Returns the inverse of a given number. 685 686=head2 @constants = B<Math::NumberCruncher::CONSTANTS>( 'all' [, $decimal_places] ); 687 688A variety of relatively common constants pre-calculated out to 2000 decimal places. For backwards compatibility, $PI, $_e_, and $_g_ will always be available without invoking CONSTANTS(), but all future pre-calculated constants will be available through here. The constants can be called individually by name, or you can specify 'all' and have all available constants returned as an array. The available constant names are '_gm_' (Golden Mean); '_catalan_' (Catalan constant); '_apery_' (Apery constant); '_landau_' or '_ramanujan_' (Landau-Ramanujan constant); '_khintchine_' (Kintchine constant); '_sierpinski_' (Sierpinski constant); '_wilbraham_' or '_gibbs_' (Wilbraham-Gibbs constant); '_gamma_' (Euler's constant, gamma); '_sqrt2_' (square root of 2); '_sqrt3_'(square root of 3); '_sqrt5_' (square root of 5). For example: $gamma = Math::NumberCruncher::CONSTANT( '_gamma_', 75 ) will return Euler's constant, gamma, out to 75 decimal places. 689 690=head2 $bernoulli = B<Math::NumberCruncher::Bernoulli>( $num [, $decimal_places] ); 691 692Bernoulli numbers according to the modern definition, sometimes called the "even-index" Bernoulli numbers. The first 498 Bernoulli numbers are cached. Only even numbers can be passed to Bernoulli(). Odd numbers, negative numbers, or numbers less than 2 return undef. Bernoulli() can be called in either scalar or list context. In scalar context, it returns the value. In list context, it returns the two numbers which, when the first is divided by the second, yields the same value as that given in scalar context. 693 694=head1 AUTHOR 695 696Kurt Kincaid, sifukurt@yahoo.com 697 698=head1 COPYRIGHT 699 700Copyright (c) 2002, Kurt Kincaid. All rights reserved. This code is free software; you can redistribute it and/or 701modify it under the same terms as Perl itself. Several of the algorithms contained herein are adapted from _Mastering 702Algorithms with Perl_, by Jon Orwant, Jarkko Hietaniemi, and John Macdonald. Copyright (c) 1999 O-Reilly & Associates, 703Inc. 704 705=head1 SPECIAL THANKS 706 707Thanks to Douglas Wilson for allowing me to borrow his code for Ln(), Exp(), Root2(), and the various other supporting 708functions. Mr. Wilson's code is based on an algorithm described at L<http://www.geocities.com/zabrodskyvlada/aat/> 709 710I would also like to thank the folks at L<http://www.perlmonks.org> for their input on optimizing B<Root()>. 711 712=head1 SEE ALSO 713 714perl(1). 715 716=cut 717