User Contributed Notes Mathematical Functions |
|
11-Aug-2000 01:55 |
|
Converting non-standard form:
you can use something like this:
$v=0.3e-9;
$v=sprintf ( "%2.9f", $v);
|
|
19-Feb-2001 09:43 |
|
for those looking for a credit card verification function i wrote a simple
LUHN Formula algorithm:
$valid = 1;
$numOfDigits = 0 - strlen($ccNumber);
$i = -1;
while ($i>=$numOfDigits){
if (($i % 2) == 0){
$double = 2*(substr($ccNumber, $i, 1));
$total += substr($double,0,1);
if (strlen($double > 1)){
$total += substr($double,1,1);
}
} else {
$total += substr($ccNumber, $i, 1);
}
$i--;
}
if (($total % 10) != 0){
$valid = 0;
}
|
|
02-Mar-2001 05:02 |
|
Here is a pretty easy way to convert someone's weight in pounds to the
English stones and pounds:
$WeightStones = floor($weight/14);
$WeightPounds = ($weight%14);
echo $WeightStones."st ".$WeightPounds."lbs
(".$weight."lbs)";
If the person weighs 136lbs, "9st 10lbs" will be returned.
Hope this helps someone...
|
|
12-Jun-2001 08:03 |
|
I found that when dealing with tables, a 'least common multiple'
function is sometimes useful for abusing tablespan and the likes.
So here goes (you may choose to remove the first part of the gcd
function if the function call is well-behaved):
function gcd(n, m) //greatest common divisor
{
n=abs(n); m=abs(m);
if (n==0 and m==0)
return 1; //avoid infinite recursion
if (n==m and n>=1)
return n;
return m<n?gcd(n-m,n):gcd(n,m-n);
}
function lcm(n, m) //least common multiple
{
return m*(n/gcd(n,m));
}
This may or may not be something to consider adding to the mathematical
function library.
|
|
webkid%webkid.com
31-May-2002 09:49 |
|
I found it kind of irritating that PHP had no native functionality for a
calculating Factorials. Since I really didn't feel like loading the GMP
library, I figured I'd write my own function.
function fact($s){$r=(int)$s; for ($i=$r;$i--;$i>1){$r=$r*$i;} return
$r;}
I think that's right... I havn't tested it extensively but it should work.
|
|
webkid%webkid.com
31-May-2002 09:54 |
|
And the reason I needed a Factorial function is because I there were no nPr
or nCr functions native to PHP, either.
function n_pick_r($n,$r){$n=(int)$n; $r=(int)$r;return
(fact($n)/fact($n-$r));}
function n_choose_r($n,$r){$n=(int)$n; $r=(int)$r;return
(n_pick_r($n,$r)/fact($r));}
Hope that helps someone!
|
|
30-Jun-2002 09:10 |
|
it's in swedish (scripts are short and easy to see). but i think that
thease functions should be implemented into PHP!
|
|
03-Jul-2002 09:40 |
|
Here are the functions that where listed in link above (they got lost).
# From Celsius
// To Fahrenheit
function celfar($c) {
return $c*1.8+32; }
// To Kelvin
function celkel($c) {
return $c+273.15; }
// To Rankine
function celran($c) {
return $c*1.8+491.67; }
// To R�aumur
function celrea($c) {
return $c*0.8; }
# From Fahrenheit
// To Celsius
function farcel($f) {
return ($f-32)*(5/9); }
// To Kelvin
function farkel($f) {
return ($f-32)*(5/9)+273.15; }
// To Rankine
function farran($f) {
return $f+459.67; }
// To R�aumur
function farrea($f) {
return ($f-32)*(4/9); }
# From Kelvin
// To Celsius
function kelcel($k) {
return $k-273.15; }
// To Fahrenheit
function kelfar($k) {
return ($k-273.15)*9/5+32; }
// To Rankine
function kelran($k) {
return $k*1.8; }
// To R�aumur
function kelrea($k) {
return ($k-273.15)*0.8; }
# From Rankine
// To Celsius
function rancel($r) {
return ($r*(5/9))-273.15; }
// To Fahrenheit
function ranfar($r) {
return $r-459.67; }
// To Kelvin
function rankel($r) {
return $r*(5/9); }
// To R�aumur
function ranrea($r) {
return ($r*(4/9))-218.52; }
# From R�aumur
// To Celsius
function reacel($re) {
return $re*1.25; }
// To Fahrenheit
function reafar($re) {
return $re*2.25+32; }
// To Kelvin
function reakel($re) {
return $re*1.25+273.15; }
// To Rankine
function rearan($re) {
return $re*2.25+491.67; }
|
|
|