px | top | add code | search | signup | login | help |
<?php
/*
** mathlib v1.1 04/21/98 nbastin
**
** MathLib for PHP3 maintained by Nick Bastin <nbastin@rbbsystems.com>
** If you have source for functions that you would like to see added, or
** have functions that you would like to see added but don't know how to
** write yourself, please email me.
**
** All functions tested before distribution for functionality, and a
** reasonable bit of accuracy. However, these functions have
** not undergone extensive testing for their precision and accuracy, and
** you use at your own risk.
**
** That said, quite a few errors did turn up in testing, and I think I
** got them all. However, if you find an error, please let me know.
**
** To use these functions, include them in your PHP scripts
**
** - Nick Bastin <nbastin@rbbsystems.com>
*/
/*
** There's already a 'round' function in PHP...do we need this?
** Author: Bjorn Borud, Guardian Networks AS, <borud@guardian.no>
*/
function roundoff($v) {
if ( $v - floor($v) >= 0.5) {
return(ceil($v));
} else {
return(floor($v));
}
}
/*
** Converts degrees to radians
** Author: Bjorn Borud, Guardian Networks AS, <borud@guardian.no>
** Modified to break reliance on PI variable by Nick Bastin, <nbastin@rbbsystems.com>
*/
function deg2rad($degrees) {
return ((pi(void) * $degrees) / doubleval(180));
}
/*
** Converts radians to degrees
** Author: Nick Bastin, RBB Systems, <nbastin@rbbsystems.com>
*/
function rad2deg($radians) {
return (($radians * doubleval(180)) / pi(void));
}
/*
** Base-$base logarithm of $val. Useful for those evaluating logarithms in non-standard bases
** Author: Nick Bastin, RBB Systems, <nbastin@rbbsystems.com>
*/
function baselog($base,$val) {
return ((log10($val))/(log10($base)));
}
/*
** MOD function - This is useful for those people who didn't compile PHP with the --enable-bcmath flag
** Author: Nick Bastin, RBB Systems, <nbastin@rbbsystems.com>
*/
function mod($divisor,$number) {
return ($number - ((floor($number/$divisor)) * $divisor));
}
/*
** Factorial function (Be careful, this number can grow out of control very quickly)
** Since you can't perform a factorial of a non-integer, the function casts $number to an integer.
** Author: Nick Bastin, RBB Systems, <nbastin@rbbsystems.com>
*/
function factorial($number) {
$a = $b = (int) $number;
while ($b>1) {
$b = $b - 1;
$a = $a * $b;
}
return $a;
}
/*
** Functions coming soon:
**
** - Infinitely precise division function
** - Factoring function
** - Prime number evaluation
** - Prime number generator
*/
?>
Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.