| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file was originally part of brick/math
|
|
|
5 |
*
|
|
|
6 |
* Copyright (c) 2013-present Benjamin Morel
|
|
|
7 |
*
|
|
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
9 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
10 |
* in the Software without restriction, including without limitation the rights
|
|
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
12 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
13 |
* furnished to do so, subject to the following conditions:
|
|
|
14 |
*
|
|
|
15 |
* The above copyright notice and this permission notice shall be included in
|
|
|
16 |
* all copies or substantial portions of the Software.
|
|
|
17 |
*
|
|
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
24 |
* SOFTWARE.
|
|
|
25 |
*
|
|
|
26 |
* @link https://github.com/brick/math brick/math at GitHub
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
declare(strict_types=1);
|
|
|
30 |
|
|
|
31 |
namespace Ramsey\Uuid\Math;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Specifies a rounding behavior for numerical operations capable of discarding
|
|
|
35 |
* precision.
|
|
|
36 |
*
|
|
|
37 |
* Each rounding mode indicates how the least significant returned digit of a
|
|
|
38 |
* rounded result is to be calculated. If fewer digits are returned than the
|
|
|
39 |
* digits needed to represent the exact numerical result, the discarded digits
|
|
|
40 |
* will be referred to as the discarded fraction regardless the digits'
|
|
|
41 |
* contribution to the value of the number. In other words, considered as a
|
|
|
42 |
* numerical value, the discarded fraction could have an absolute value greater
|
|
|
43 |
* than one.
|
|
|
44 |
*/
|
|
|
45 |
final class RoundingMode
|
|
|
46 |
{
|
|
|
47 |
/**
|
|
|
48 |
* Private constructor. This class is not instantiable.
|
|
|
49 |
*
|
|
|
50 |
* @codeCoverageIgnore
|
|
|
51 |
*/
|
|
|
52 |
private function __construct()
|
|
|
53 |
{
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Asserts that the requested operation has an exact result, hence no
|
|
|
58 |
* rounding is necessary.
|
|
|
59 |
*/
|
|
|
60 |
public const UNNECESSARY = 0;
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Rounds away from zero.
|
|
|
64 |
*
|
|
|
65 |
* Always increments the digit prior to a nonzero discarded fraction.
|
|
|
66 |
* Note that this rounding mode never decreases the magnitude of the
|
|
|
67 |
* calculated value.
|
|
|
68 |
*/
|
|
|
69 |
public const UP = 1;
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Rounds towards zero.
|
|
|
73 |
*
|
|
|
74 |
* Never increments the digit prior to a discarded fraction (i.e.,
|
|
|
75 |
* truncates). Note that this rounding mode never increases the magnitude of
|
|
|
76 |
* the calculated value.
|
|
|
77 |
*/
|
|
|
78 |
public const DOWN = 2;
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Rounds towards positive infinity.
|
|
|
82 |
*
|
|
|
83 |
* If the result is positive, behaves as for UP; if negative, behaves as for
|
|
|
84 |
* DOWN. Note that this rounding mode never decreases the calculated value.
|
|
|
85 |
*/
|
|
|
86 |
public const CEILING = 3;
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Rounds towards negative infinity.
|
|
|
90 |
*
|
|
|
91 |
* If the result is positive, behave as for DOWN; if negative, behave as for
|
|
|
92 |
* UP. Note that this rounding mode never increases the calculated value.
|
|
|
93 |
*/
|
|
|
94 |
public const FLOOR = 4;
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant,
|
|
|
98 |
* in which case round up.
|
|
|
99 |
*
|
|
|
100 |
* Behaves as for UP if the discarded fraction is >= 0.5; otherwise, behaves
|
|
|
101 |
* as for DOWN. Note that this is the rounding mode commonly taught at
|
|
|
102 |
* school.
|
|
|
103 |
*/
|
|
|
104 |
public const HALF_UP = 5;
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant,
|
|
|
108 |
* in which case round down.
|
|
|
109 |
*
|
|
|
110 |
* Behaves as for UP if the discarded fraction is > 0.5; otherwise, behaves
|
|
|
111 |
* as for DOWN.
|
|
|
112 |
*/
|
|
|
113 |
public const HALF_DOWN = 6;
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant,
|
|
|
117 |
* in which case round towards positive infinity.
|
|
|
118 |
*
|
|
|
119 |
* If the result is positive, behaves as for HALF_UP; if negative, behaves
|
|
|
120 |
* as for HALF_DOWN.
|
|
|
121 |
*/
|
|
|
122 |
public const HALF_CEILING = 7;
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Rounds towards "nearest neighbor" unless both neighbors are equidistant,
|
|
|
126 |
* in which case round towards negative infinity.
|
|
|
127 |
*
|
|
|
128 |
* If the result is positive, behaves as for HALF_DOWN; if negative, behaves
|
|
|
129 |
* as for HALF_UP.
|
|
|
130 |
*/
|
|
|
131 |
public const HALF_FLOOR = 8;
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Rounds towards the "nearest neighbor" unless both neighbors are
|
|
|
135 |
* equidistant, in which case rounds towards the even neighbor.
|
|
|
136 |
*
|
|
|
137 |
* Behaves as for HALF_UP if the digit to the left of the discarded fraction
|
|
|
138 |
* is odd; behaves as for HALF_DOWN if it's even.
|
|
|
139 |
*
|
|
|
140 |
* Note that this is the rounding mode that statistically minimizes
|
|
|
141 |
* cumulative error when applied repeatedly over a sequence of calculations.
|
|
|
142 |
* It is sometimes known as "Banker's rounding", and is chiefly used in the
|
|
|
143 |
* USA.
|
|
|
144 |
*/
|
|
|
145 |
public const HALF_EVEN = 9;
|
|
|
146 |
}
|