2015년 2월 4일 수요일

PHP 수학 연산 함수 정리


/*************************************
PHP 수학 연산 함수 정리
http://php.net/manual/en/ref.math.php
**************************************/

/*
숫자의 절대값을 반환
abs
number = abs(mixed_number)
http://php.net/manual/en/function.abs.php
*/
echo abs(-4.2); // 4.2 (double/float)
echo abs(5);    // 5 (integer)
echo abs(-5);   // 5 (integer)

/*
숫자의 반올림한 값을 반환
round
float = round(float_val)
http://php.net/manual/en/function.round.php
PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN
PHP_ROUND_HALF_EVEN
PHP_ROUND_HALF_ODD
*/
echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.05
echo round(5.055, 2);    // 5.06
echo round(9.5, 0, PHP_ROUND_HALF_UP);   // 10
echo round(9.5, 0, PHP_ROUND_HALF_DOWN); // 9
echo round(9.5, 0, PHP_ROUND_HALF_EVEN); // 10
echo round(9.5, 0, PHP_ROUND_HALF_ODD);  // 9
echo round(8.5, 0, PHP_ROUND_HALF_UP);   // 9
echo round(8.5, 0, PHP_ROUND_HALF_DOWN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_EVEN); // 8
echo round(8.5, 0, PHP_ROUND_HALF_ODD);  // 9
/* Using PHP_ROUND_HALF_UP with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_UP);   //  1.6
echo round( 1.54, 1, PHP_ROUND_HALF_UP);   //  1.5
echo round(-1.55, 1, PHP_ROUND_HALF_UP);   // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_UP);   // -1.5
/* Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_DOWN); //  1.5
echo round( 1.54, 1, PHP_ROUND_HALF_DOWN); //  1.5
echo round(-1.55, 1, PHP_ROUND_HALF_DOWN); // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_DOWN); // -1.5
/* Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_EVEN); //  1.6
echo round( 1.54, 1, PHP_ROUND_HALF_EVEN); //  1.5
echo round(-1.55, 1, PHP_ROUND_HALF_EVEN); // -1.6
echo round(-1.54, 1, PHP_ROUND_HALF_EVEN); // -1.5
/* Using PHP_ROUND_HALF_ODD with 1 decimal digit precision */
echo round( 1.55, 1, PHP_ROUND_HALF_ODD);  //  1.5
echo round( 1.54, 1, PHP_ROUND_HALF_ODD);  //  1.5
echo round(-1.55, 1, PHP_ROUND_HALF_ODD);  // -1.5
echo round(-1.54, 1, PHP_ROUND_HALF_ODD);  // -1.5

/*
인자로 전달받은 부동소수형의 값보다 더 큰 정수 중 최소값을 반환
ceil
float = ceil(float_value)
http://php.net/manual/en/function.ceil.php
*/
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3

/*
인자로 전달받은 부동소수형의 값보다 더 작은 정수 중 최대값을 반환
floor
float = floor(float_value)
http://php.net/manual/en/function.floor.php
*/
echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4

/*
제곱승을 반환
pow
number = pow ( number $base , number $exp )
http://php.net/manual/en/function.pow.php
*/
var_dump(pow(2, 8)); // int(256)
echo pow(-1, 20); // 1
echo pow(0, 0); // 1

echo pow(-1, 5.5); // PHP >4.0.6  NAN
echo pow(-1, 5.5); // PHP <=4.0.6 1.#IND

/*
e를 지수로 한 제곱승을 반환
exp
float = exp ( float $arg )
http://php.net/manual/en/function.exp.php
*/
echo exp(12) . "\n";
echo exp(5.7);

/*
지수가 e인 자연로그 값을 반환
log
float = log ( float $arg [, float $base = M_E ] )
http://php.net/manual/en/function.log.php
*/

/*
지수가 10인 상용로그 값을 반환
log10
float = log10 ( float $arg )
http://php.net/manual/en/function.log10.php
*/

/*
원주율 파이의 값을 반환
pi
float = pi ( void )
http://php.net/manual/en/function.pi.php
*/
echo pi(); // 3.1415926535898
echo M_PI; // 3.1415926535898

/*
제곱근을 반환
sqrt
float = sqrt ( float $arg )
http://php.net/manual/en/function.sqrt.php
*/
// Precision depends on your precision directive
echo sqrt(9); // 3
echo sqrt(10); // 3.16227766 ...

/*
인자로 전달받은 값 중 가장 큰 값을 반환
max
mixed = max ( array $values )
mixed = max ( mixed $value1 , mixed $value2 [, mixed $... ] )
http://php.net/manual/en/function.max.php
*/
echo max(2, 3, 1, 6, 7);  // 7
echo max(array(2, 4, 5)); // 5

// The string 'hello' when compared to an int is treated as 0
// Since the two values are equal, the order they are provided determines the result
echo max(0, 'hello');     // 0
echo max('hello', 0);     // hello

// Here we are comparing -1 < 0, so 'hello' is the highest value
echo max('hello', -1);    // hello

// With multiple arrays of different lengths, max returns the longest
$val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1)

// Multiple arrays of the same length are compared from left to right
// so in our example: 2 == 2, but 5 > 4
$val = max(array(2, 4, 8), array(2, 5, 1)); // array(2, 5, 1)

// If both an array and non-array are given, the array will be returned
// as comparisons treat arrays as greater than any other value
$val = max('string', array(2, 5, 7), 42);   // array(2, 5, 7)

// If one argument is NULL or a boolean, it will be compared against
// other values using the rule FALSE < TRUE regardless of the other types involved
// In the below examples, -10 is treated as TRUE in the comparison
$val = max(-10, FALSE); // -10
$val = max(-10, FALSE); // -10

// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE
$val = max(0, TRUE); // TRUE

/*
인자로 전달받은 값 중 가장 작은 값을 반환
min
mixed = min ( array $values )
mixed = min ( mixed $value1 , mixed $value2 [, mixed $... ] )
http://php.net/manual/en/function.min.php
*/
echo min(2, 3, 1, 6, 7);  // 1
echo min(array(2, 4, 5)); // 2

// The string 'hello' when compared to an int is treated as 0
// Since the two values are equal, the order they are provided determines the result
echo min(0, 'hello');     // 0
echo min('hello', 0);     // hello

// Here we are comparing -1 < 0, so -1 is the lowest value
echo min('hello', -1);    // -1

// With multiple arrays of different lengths, min returns the shortest
$val = min(array(2, 2, 2), array(1, 1, 1, 1)); // array(2, 2, 2)

// Multiple arrays of the same length are compared from left to right
// so in our example: 2 == 2, but 4 < 5
$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)

// If both an array and non-array are given, the array is never returned
// as comparisons treat arrays as greater than any other value
$val = min('string', array(2, 5, 7), 42);   // string

// If one argument is NULL or a boolean, it will be compared against
// other values using the rule FALSE < TRUE regardless of the other types involved
// In the below examples, both -10 and 10 are treated as TRUE in the comparison
$val = min(-10, FALSE, 10); // FALSE
$val = min(-10, NULL, 10);  // NULL

// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE
$val = min(0, TRUE); // 0

/*
인자로 지정한 최소값과 최대값 사이의 임의의 난수를 반환
rand
int = rand ( void )
int = rand ( int $min , int $max )
http://php.net/manual/en/function.rand.php
*/
echo rand();
echo rand();

echo rand(5, 15);

/*
Mersenne Twister 알고리즘을 이용하여 인자로 지정한 최소값과 최대값 사이의 임의의 난수를 반환
mt_rand
int = mt_rand ( void )
int = mt_rand ( int $min , int $max )
http://php.net/manual/en/function.mt-rand.php
*/
echo mt_rand();
echo mt_rand();

echo mt_rand(5, 15);

댓글 없음:

댓글 쓰기

플러터 단축키

1. 위젯 감싸기/벗기기 비주얼 스튜디오 :   Cmd + . 안드로이드 스튜디오 : Alt + Enter 2. 코드 정렬 비주얼 스튜디오 : Ctrl + S 안드로이드 스튜디오 : Ctlr + Alt + L 3. StatelessWidget ->...