| Total Complexity | 12 |
| Total Lines | 58 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 16 | trait FMAT |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * 返回数的阶乘 |
||
| 20 | * |
||
| 21 | * @param integer|double $number 必需。 要计算其阶乘的非负数。 如果 number 不是整数,将被截尾取整 |
||
| 22 | * |
||
| 23 | * @return integer|false |
||
| 24 | */ |
||
| 25 | 4 | public static function fact($number) |
|
| 26 | { |
||
| 27 | 4 | $number = (int) floor($number); |
|
| 28 | 4 | if ($number == 0) { |
|
| 29 | 1 | return 1; |
|
| 30 | 4 | } elseif ($number < 0) { |
|
| 31 | 1 | return false; |
|
| 32 | } |
||
| 33 | 4 | $result = 1; |
|
| 34 | 4 | foreach (Arrays::rangeGenerator(1, $number) as $num) { |
|
| 35 | 4 | $result *= $num; |
|
| 36 | } |
||
| 37 | 4 | return $result; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * 返回数字的双倍阶乘 |
||
| 42 | * |
||
| 43 | * - 偶数:n!! = n * (n - 2) * (n - 4) * ... * 4 * 2 |
||
| 44 | * - 奇数:n!! = n * (n - 2) * (n - 4) * ... * 3 * 1 |
||
| 45 | * |
||
| 46 | * @param integer $number 必需。 为其返回双倍阶乘的值。 如果 number 不是整数,将被截尾取整 |
||
| 47 | * |
||
| 48 | * @return integer |
||
| 49 | */ |
||
| 50 | 1 | public static function factdouble($number) |
|
| 51 | { |
||
| 52 | 1 | if ($number == 0) { |
|
| 53 | 1 | return 1; |
|
| 54 | } |
||
| 55 | 1 | $result = 1; |
|
| 56 | 1 | $isEven = $number % 2 == 0; |
|
| 57 | 1 | foreach (Arrays::rangeGenerator($isEven ? 2 : 1, (int) $number, 2) as $num) { |
|
| 58 | 1 | $result *= $num; |
|
| 59 | } |
||
| 60 | 1 | return $result; |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * 将参数 number 向下舍入(沿绝对值减小的方向)为最接近的 significance 的倍数 |
||
| 65 | * |
||
| 66 | * @param double $number 必需。 要舍入的数值 |
||
| 67 | * @param double $significance 必需。 要舍入到的倍数 |
||
| 68 | * |
||
| 69 | * @return double|false |
||
| 70 | */ |
||
| 71 | 1 | public static function floor($number, $significance = 1) |
|
| 74 | } |
||
| 75 | } |
||
| 76 |