Math Functions
Table of Contents
- abs — Absolute value
- acos — Arc cosine
- acosh — Inverse hyperbolic cosine
- asin — Arc sine
- asinh — Inverse hyperbolic sine
- atan2 — Arc tangent of two variables
- atan — Arc tangent
- atanh — Inverse hyperbolic tangent
- base_convert — Convert a number between arbitrary bases
- bindec — Binary to decimal
- ceil — Round fractions up
- cos — Cosine
- cosh — Hyperbolic cosine
- decbin — Decimal to binary
- dechex — Decimal to hexadecimal
- decoct — Decimal to octal
- deg2rad — Converts the number in degrees to the radian equivalent
- exp — Calculates the exponent of e
- expm1 — Returns exp(number) - 1, computed in a way that is accurate even
when the value of number is close to zero
- floor — Round fractions down
- fmod — Returns the floating point remainder (modulo) of the division
of the arguments
- getrandmax — Show largest possible random value
- hexdec — Hexadecimal to decimal
- hypot — Calculate the length of the hypotenuse of a right-angle triangle
- is_finite — Finds whether a value is a legal finite number
- is_infinite — Finds whether a value is infinite
- is_nan — Finds whether a value is not a number
- lcg_value — Combined linear congruential generator
- log10 — Base-10 logarithm
- log1p — Returns log(1 + number), computed in a way that is accurate even when
the value of number is close to zero
- log — Natural logarithm
- max — Find highest value
- min — Find lowest value
- mt_getrandmax — Show largest possible random value
- mt_rand — Generate a better random value
- mt_srand — Seed the better random number generator
- octdec — Octal to decimal
- pi — Get value of pi
- pow — Exponential expression
- rad2deg — Converts the radian number to the equivalent number in degrees
- rand — Generate a random integer
- round — Rounds a float
- sin — Sine
- sinh — Hyperbolic sine
- sqrt — Square root
- srand — Seed the random number generator
- tan — Tangent
- tanh — Hyperbolic tangent
Коментарии
For people interest in Differential Equations, I've done a function that receive a string like: x^2+x^3 and put it in
2x+3x^2 witch is the differantial of the previous equation.
In the code there is one thing missing: the $string{$i} is often going outOfBound (Uninitialized string offset: 6 in...)
if your error setting is set a little too high... I just dont know how to fix this.
So there is the code for differential equation with (+ and -) only:
<?
function differentiel($equa)
{
$equa = strtolower($equa);
echo "Equation de depart: ".$equa."<br>";
$final = "";
for($i = 0; $i < strlen($equa); $i++)
{
//Make a new string from the receive $equa
if($equa{$i} == "x" && $equa{$i+1} == "^")
{
$final .= $equa{$i+2};
$final .= "x^";
$final .= $equa{$i+2}-1;
}
elseif($equa{$i} == "+" || $equa{$i} == "-")
{
$final .= $equa{$i};
}
elseif(is_numeric($equa{$i}) && $i == 0)
{
//gerer parenthese et autre terme generaux + gerer ^apres: 2^2
$final .= $equa{$i}."*";
}
elseif(is_numeric($equa{$i}) && $i > 0 && $equa{$i-1} != "^")
{
//gerer ^apres: 2^2
$final .= $equa{$i}."*";
}
elseif($equa{$i} == "^")
{
continue;
}
elseif(is_numeric($equa{$i}) && $equa{$i-1} == "^")
{
continue;
}
else
{
if($equa{$i} == "x")
{
$final .= 1;
}
else
{
$final .= $equa{$i};
}
}
}
//
//Manage multiplication add in the previous string $final
//
$finalMul = "";
for($i = 0; $i < strlen($final); $i++)
{
if(is_numeric($final{$i}) && $final{$i+1} == "*" && is_numeric($final{$i+2}))
{
$finalMul .= $final{$i}*$final{$i+2};
}
elseif($final{$i} == "*")
{
continue;
}
elseif(is_numeric($final{$i}) && $final{$i+1} != "*" && $final{$i-1} == "*")
{
continue;
}
else
{
$finalMul .= $final{$i};
}
}
echo "equa final: ".$finalMul;
}
?>
I know this is not optimal but i've done this quick :)
If you guys have any comment just email me.
I also want to do this fonction In C to add to phpCore maybe soon...
Patoff
If you need to deal with polar co-ordinates for somereason you will need to convert to and from x,y for input and output in most situations: here are some functions to convert cartesian to polar and polar to cartesian
<?
//returns array of r, theta in the range of 0-2*pi (in radians)
function rect2polar($x,$y)
{
if(is_numeric($x)&&is_numeric($y))
{
$r=sqrt(pow($x,2)+pow($y,2));
if($x==0)
{
if($y>0) $theta=pi()/2;
else $theta=3*pi()/2;
}
else if($x<0) $theta=atan($y/$x)+pi();
else if($y<0) $theta=atan($y/$x)+2*pi();
else $theta=atan($y/$x);
$polar=array("r"=>$r,"theta"=>$theta);
return $polar;
}
else return false;
}
//r must be in radians, returns array of x,y
function polar2rect($r,$theta)
{
if(is_numeric($r)&&is_numeric($theta))
{
$x=$r*cos($theta);
$y=$r*sin($theta);
$rect=array("x"=>$x,"y"=>$y);
}
else
{
return false;
}
}
?>
If you're an aviator and needs to calculate windcorrection angles and groundspeed (e.g. during flightplanning) this can be very useful.
$windcorrection = rad2deg(asin((($windspeed * (sin(deg2rad($tt - ($winddirection-180))))/$tas))));
$groundspeed = $tas*cos(deg2rad($windcorrection)) + $windspeed*cos(deg2rad($tt-($winddirection-180)));
You can probably write these lines more beautiful, but they work!
Wouldn't the following function do the same but a lot easier than the one in the comment before?
function trimInteger($targetNumber,$newLength) {
return $targetNumber%pow(10,$newLength);
}