Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Component\Utility;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Provides helper methods for manipulating numbers.
|
Chris@0
|
7 *
|
Chris@0
|
8 * @ingroup utility
|
Chris@0
|
9 */
|
Chris@0
|
10 class Number {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Verifies that a number is a multiple of a given step.
|
Chris@0
|
14 *
|
Chris@0
|
15 * The implementation assumes it is dealing with IEEE 754 double precision
|
Chris@0
|
16 * floating point numbers that are used by PHP on most systems.
|
Chris@0
|
17 *
|
Chris@0
|
18 * This is based on the number/range verification methods of webkit.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @param float $value
|
Chris@0
|
21 * The value that needs to be checked.
|
Chris@0
|
22 * @param float $step
|
Chris@0
|
23 * The step scale factor. Must be positive.
|
Chris@0
|
24 * @param float $offset
|
Chris@0
|
25 * (optional) An offset, to which the difference must be a multiple of the
|
Chris@0
|
26 * given step.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @return bool
|
Chris@0
|
29 * TRUE if no step mismatch has occurred, or FALSE otherwise.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @see http://opensource.apple.com/source/WebCore/WebCore-1298/html/NumberInputType.cpp
|
Chris@0
|
32 */
|
Chris@0
|
33 public static function validStep($value, $step, $offset = 0.0) {
|
Chris@0
|
34 $double_value = (double) abs($value - $offset);
|
Chris@0
|
35
|
Chris@0
|
36 // The fractional part of a double has 53 bits. The greatest number that
|
Chris@0
|
37 // could be represented with that is 2^53. If the given value is even bigger
|
Chris@0
|
38 // than $step * 2^53, then dividing by $step will result in a very small
|
Chris@0
|
39 // remainder. Since that remainder can't even be represented with a single
|
Chris@0
|
40 // precision float the following computation of the remainder makes no sense
|
Chris@0
|
41 // and we can safely ignore it instead.
|
Chris@0
|
42 if ($double_value / pow(2.0, 53) > $step) {
|
Chris@0
|
43 return TRUE;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 // Now compute that remainder of a division by $step.
|
Chris@0
|
47 $remainder = (double) abs($double_value - $step * round($double_value / $step));
|
Chris@0
|
48
|
Chris@0
|
49 // $remainder is a double precision floating point number. Remainders that
|
Chris@0
|
50 // can't be represented with single precision floats are acceptable. The
|
Chris@0
|
51 // fractional part of a float has 24 bits. That means remainders smaller than
|
Chris@0
|
52 // $step * 2^-24 are acceptable.
|
Chris@0
|
53 $computed_acceptable_error = (double) ($step / pow(2.0, 24));
|
Chris@0
|
54
|
Chris@0
|
55 return $computed_acceptable_error >= $remainder || $remainder >= ($step - $computed_acceptable_error);
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Generates a sorting code from an integer.
|
Chris@0
|
60 *
|
Chris@0
|
61 * Consists of a leading character indicating length, followed by N digits
|
Chris@0
|
62 * with a numerical value in base 36 (alphadecimal). These codes can be sorted
|
Chris@0
|
63 * as strings without altering numerical order.
|
Chris@0
|
64 *
|
Chris@0
|
65 * It goes:
|
Chris@0
|
66 * 00, 01, 02, ..., 0y, 0z,
|
Chris@0
|
67 * 110, 111, ... , 1zy, 1zz,
|
Chris@0
|
68 * 2100, 2101, ..., 2zzy, 2zzz,
|
Chris@0
|
69 * 31000, 31001, ...
|
Chris@0
|
70 *
|
Chris@0
|
71 * @param int $i
|
Chris@0
|
72 * The integer value to convert.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @return string
|
Chris@0
|
75 * The alpha decimal value.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @see \Drupal\Component\Utility\Number::alphadecimalToInt
|
Chris@0
|
78 */
|
Chris@0
|
79 public static function intToAlphadecimal($i = 0) {
|
Chris@0
|
80 $num = base_convert((int) $i, 10, 36);
|
Chris@0
|
81 $length = strlen($num);
|
Chris@0
|
82
|
Chris@0
|
83 return chr($length + ord('0') - 1) . $num;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Decodes a sorting code back to an integer.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param string $string
|
Chris@0
|
90 * The alpha decimal value to convert
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return int
|
Chris@0
|
93 * The integer value.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @see \Drupal\Component\Utility\Number::intToAlphadecimal
|
Chris@0
|
96 */
|
Chris@0
|
97 public static function alphadecimalToInt($string = '00') {
|
Chris@0
|
98 return (int) base_convert(substr($string, 1), 36, 10);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|