This repository was archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath.php
69 lines (63 loc) · 2.11 KB
/
Math.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php namespace nyx\utils;
/**
* Math
*
* Utilities related to mathematical functions and working with numbers.
*
* @package Nyx\Utils\Math
* @version 0.0.1
* @author Michal Chojnacki <[email protected]>
* @copyright 2012-2016 Nyx Dev Team
* @link http://docs.muyo.io/nyx/utils/math.html
*/
class Math
{
/**
* The traits of the Math class.
*/
use traits\StaticallyExtendable;
/**
* Mathematical constants map.
*/
const CONSTANTS = [
'pi' => M_PI,
'e' => M_E,
'euler-mascheroni' => M_EULER,
'conway' => 1.303577269
];
/**
* Rounding constants.
*/
const ROUND_ZERO = 0;
const ROUND_PLUSINF = 1;
const ROUND_MINUSINF = 2;
/**
* Returns the number of decimal places contained in the given number.
*
* @param float $value The number to count the decimal places of.
* @return int|bool The number of decimal places or false if the given $value was not numeric.
*/
public static function countDecimals(float $value)
{
// When the value when cast to an integer is about the same, return 0 decimal places. Otherwise count them.
return (int) $value == $value ? 0 : (strlen($value) - strrpos($value, '.') - 1);
}
/**
* Checks whether the given value is a mathematical constant (one of self::$constants) and returns its name
* when it is.
*
* @param float $value The number to check.
* @param int $precision The decimal precision of the check, 4 at minimum.
* @return string The name of the constant (one of the keys of self::$constants) or null if
* the given value is not a constant.
*/
public static function detectConstant(float $value, int $precision = 6)
{
foreach (static::CONSTANTS as $name => $constant) {
if (0 === bccomp($value, $constant, max($precision - 1, 4))) {
return $name;
}
}
return null;
}
}