PHP/Math/Math
Содержание
Create and use a simple static Math() class
<source lang="html4strict">
<?php define("RAND_MAX", mt_getrandmax()); class Math {
static $pi = M_PI; static $e = M_E; static function pi() { return M_PI; } static function intval($val) { return intval($val); } static function floor($val) { return floor($val); } static function ceil($val) { return ceil($val); } static function round($val, $decimals = 0) { return round($val, $decimals); } static function abs($val) { return abs($val); } static function floatval($val) { return floatval($val); } static function rand($min = 0, $max = RAND_MAX) { return mt_rand($min, $max); } static function min($var1, $var2) { return min($var1, $var2); } static function max($var1, $var2) { return max($var1, $var2); }
} $a = 3.5;
echo "Math::\$pi = " . Math::$pi . "\n"; echo "Math::\$e = " . Math::$e . "\n"; echo "Math::intval($a) = " . Math::intval($a) . "\n"; echo "Math::floor($a) = " . Math::floor($a) . "\n"; echo "Math::ceil($a) = " . Math::ceil($a) . "\n"; echo "Math::round(Math::\$pi, 2) = " . Math::round(Math::$pi, 2) . "\n"; echo "Math::abs(-$a) = " . Math::abs(-$a) . "\n"; echo "Math::floatval($a) = " . Math::floatval($a) . "\n"; echo "Math::rand(5, 25) = " . Math::rand(5, 25) . "\n"; echo "Math::rand() = " . Math::rand() . "\n"; echo "Math::min(2, 28) = " . Math::min(3, 28) . "\n"; echo "Math::max(3, 28) = " . Math::max(3, 28) . "\n"; ?>
</source>
Hyperbolic Functions
<source lang="html4strict">
Name Description cosh() Hyperbolic cosine (exp(arg) + exp(-arg))/2 sinh() Hyperbolic sine (exp(arg) - exp(-arg))/2 tanh() Hyperbolic tangent sinh(arg)/cosh(arg) acosh() Inverse hyperbolic cosine asinh() Inverse hyperbolic sine atanh() Inverse hyperbolic tangent <?php $start = array(0, 0); $end = array(100, 0); $length = sqrt(pow($end[0] - $start[0], 2) + pow($end[1] - $start[1], 2)); $angle = 35; $r = deg2rad($angle); $new_start = array(20, 20); $new_end = array( $new_start[0] + cos($r) * $length, $new_start[1] + sin($r) * $length ); var_dump($new_end); ?>
</source>
Math calculation
<source lang="html4strict">
<?
$somevar = 5 + 5; // 10 $somevar = 5 - 5; // 0 $somevar = 5 + 5 - (5 + 5); // 0 $somevar = 5 * 5; // 25 $somevar = 10 * 5 - 5; // 45 $somevar = $somevar . "appended to end"; $somevar = false; $somevar = !$somevar; // $somevar is now set to true $somevar = 5; $somevar++; // $somevar is now 6 $somevar--; // $somevar is now 5 again ++$somevar; // $somevar is 6
?>
</source>
Mathematical constants
<source lang="html4strict">
Constant Value Meaning
M_PI 3.14159265358979323846 pi
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(M_PI)
M_2_SQRTPI 1.12837916709551257390 2/sqrt(M_PI)
M_SQRT2 1.41421356237309504880 sqrt(2)
M_SQRT3 1.73205080756887729352 sqrt(3)
M_SQRT1_2 0.70710678118654752440 1/sqrt(2)
</source>
Math operations
<source lang="html4strict">
<? print 2 + 2; print 17 - 3.5; print 10 / 3; print 6 * 9; ?>
</source>