|
|
LII. Matematikai f�ggv�nyek
Ezek a matematikai f�ggv�nyek csak a g�peden �br�zolhat� integer
�s float t�pusok �rt�ktartom�ny�ba es� �rt�keket k�pes kezelni
(ez jelenleg azonos a C beli long �s double t�pusok�val). Ha nagyobb sz�mokat
kell kezelni, akkor a n�zd meg a BCMath tetsz�leges pontoss�g� matematikai f�ggv�nyeket..
Ez a matematikai kiterjeszt�s a k�vetkez� �rt�keket hozza l�tre a PHP-ban:
T�bl�zat 1. Matematikai �lland�k �lland� | �rt�k | Le�r�s |
---|
M_PI | 3.14159265358979323846 | Pi | M_E | 2.7182818284590452354 | e | M_LOG2E | 1.4426950408889634074 | log_2 e | M_LOG10E | 0.43429448190325182765 | log_10 e | M_LN2 | 0.69314718055994530942 | log_e 2 | M_LN10 | 2.30258509299404568402 | log_e 10 | M_PI_2 | 1.57079632679489661923 | pi/2 | M_PI_4 | 0.78539816339744830962 | pi/4 | M_1_PI | 0.31830988618379067154 | 1/pi | M_2_PI | 0.63661977236758134308 | 2/pi | M_SQRTPI | 1.77245385090551602729 | sqrt(pi) [4.0.2] | M_2_SQRTPI | 1.12837916709551257390 | 2/sqrt(pi) | M_SQRT2 | 1.41421356237309504880 | sqrt(2) | M_SQRT3 | 1.73205080756887729352 | sqrt(3) [4.0.2] | M_SQRT1_2 | 0.70710678118654752440 | 1/sqrt(2) | M_LNPI | 1.14472988584940017414 | log_e(pi) [4.0.2] | M_EULER | 0.57721566490153286061 | Euler constant [4.0.2] |
PHP 4.0.0 verzi�j�ig csak az M_PI haszn�lhat�.
Minden m�s �lland� PHP 4.0.0-t�l kezdve el�rhet�,
kiv�ve amelyikn�l a [4.0.2] c�mke olvashat�:
ezek csak PHP 4.0.2-t�l kezdve.
- Tartalom
- abs -- abszol�t �rt�k
- acos -- arkusz koszinusz
- acosh -- area hiperbolikus koszinusz
- asin -- arkusz szinusz
- asinh -- area hiperbolikus szinusz
- atan2 -- k�t v�ltoz� arkusz tangense
- atan -- arkusz tangens
- atanh -- area hiperbolikus tangens
- base_convert -- tetsz�leges sz�mrendszerbe v�lt �t egy sz�mot
- bindec -- bin�risb�l tizes sz�mrendszerbe v�lt �t
- ceil -- t�rtr�sszel b�r� sz�mot felfel� kerek�ti
- cos -- koszinusz
- cosh -- hiperbolikus koszinusz
- decbin -- t�zes sz�mrendszerb�l kettesbe v�lt �t
- dechex -- t�zes sz�mrendszerb�l tizenhatosba v�lt �t
- decoct -- t�zes sz�mrendszerb�l nyolcasba v�lt �t
- deg2rad -- fokb�l radi�nba v�lt �t
- exp -- e a(z) ... -re emelve
- expm1 --
exp(number) - 1 pontos �rt�ke, m�g ha a number 0-hoz k�zeli �rt�k� is
- floor -- lefel� kerek�ti a t�rtr�sszel b�r� sz�mot
- getrandmax -- a legnagyobb lehets�ges v�letlen sz�mot adja vissza
- hexdec -- tizenhatos sz�mrendszerb�l t�zesbe v�lt �t
- hypot --
a sqrt( num1*num1 + num2*num2) �rt�k�t adja vissza
- is_finite --
- is_infinite --
- is_nan --
- lcg_value -- Kombin�lt line�ris kongruencia gener�tor
- log10 -- t�zes alap� logaritmus
- log1p --
log(1 + number) pontos �rt�k�t adja vissza, akkor is ha a param�ter
�rt�ke 0-hoz k�zeli
- log -- term�szetes alap� logaritmus
- max -- megkeresi a legnagyobb �rt�ket
- min -- megkeresi a legkisebb �rt�ket
- mt_getrandmax -- visszaadja a lehets�ges legnagyobb v�letlensz�mot
- mt_rand -- v�letlensz�mot gener�l - jobban :)
- mt_srand -- be�ll�tja a "jobbik" v�letlensz�m gener�tor kiindul� �rt�k�t
- number_format -- ezres csoportos�t�ssal form�zza a sz�mot
- octdec -- nyolcas sz�mrendszerb�l t�zesbe v�lt �t
- pi -- visszaadja pi �rt�k�t
- pow -- hatv�nyoz�s
- rad2deg -- radi�nb�l fokokra v�lt
- rand -- v�letlensz�mot gener�l
- round -- lebeg�pontos sz�mot kerek�t
- sin -- szinusz
- sinh -- hiperbolikus szinusz
- sqrt -- n�gyzetgy�k�t von
- srand -- a v�letlensz�mgener�tor kiindul� �rt�k�t �ll�tja be
- tan -- tangens
- tanh -- hiperbolikus tangens
User Contributed Notes Matematikai f�ggv�nyek |
|
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; }
|
|
08-Jul-2002 07:13 |
|
<?
/**
* Function to calculate base36 values from a number. Very
* useful if you wish to generate IDs from numbers.
*
* @param $value The number
* @param $base The base to be applied (16, 36 or 64)
* @return The calculated string
* @author Shashank Tripathi ([email protected])
* @version 0.1 - Let me know if something doesnt work
*
*/
function base36($value, $base)
{
$baseChars = array('0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
);
$remainder = 0;
$newval = "";
while ( $value > 0 )
{
$remainder = $value % $base;
$value = ( ($value - $remainder)/ $base );
$newval .= $baseChars[$remainder];
}
return strrev($newval);
}
echo "The string for 46655, for instance, is " . base36(46655,
36);
?>
|
|
10-Jul-2002 11:13 |
|
I couldn't have found mod function, but it is there as % sign. For
example; 6%3=0, 7%3=1 and so on.
Oliver Lepki
|
|
|
| |