/
numbers.java
291 lines (196 loc) · 9.82 KB
/
numbers.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package accessory;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
public abstract class numbers extends parent_static
{
public static final String CONFIG_PERC_REF_LARGER = _types.CONFIG_NUMBERS_PERC_REF_LARGER;
public static final String PERC_REF_LARGER = CONFIG_PERC_REF_LARGER;
public static final double MIN_DECIMAL = -1 * Double.MAX_VALUE; //!!!
public static final long MIN_LONG = Long.MIN_VALUE;
public static final int MIN_INT = Integer.MIN_VALUE;
public static final int MIN_DIGITS_DECIMALS = 0;
public static final double MAX_DECIMAL = Double.MAX_VALUE;
public static final long MAX_LONG = Long.MAX_VALUE;
public static final int MAX_INT = Integer.MAX_VALUE;
public static final int MAX_DIGITS_DECIMAL = 308;
public static final int MAX_DIGITS_LONG = 19;
public static final int MAX_DIGITS_INT = 10;
public static final int MAX_DECIMALS = 10;
public static final double DEFAULT_DECIMAL = _defaults.NUMBERS_DECIMAL;
public static final long DEFAULT_LONG = _defaults.NUMBERS_LONG;
public static final int DEFAULT_INT = _defaults.NUMBERS_INT;
public static final int DEFAULT_DECIMALS = _defaults.NUMBERS_DECIMALS;
public static final double DEFAULT_PERC_SIMILAR = 0.1;
public static final boolean DEFAULT_PERC_REF_LARGER = true;
public static final int DEFAULT_ROUND_DECIMALS = DEFAULT_DECIMALS;
public static final RoundingMode DEFAULT_ROUND_MODE = RoundingMode.HALF_UP;
private static RoundingMode _round_mode = DEFAULT_ROUND_MODE;
private static int _round_decimals = DEFAULT_ROUND_DECIMALS;
public static final Class<?>[] get_all_classes() { return _alls.NUMBERS_CLASSES; }
public static RoundingMode get_round_mode() { return _round_mode; }
public static int get_round_decimals() { return _round_decimals; }
public static void update_round_mode(RoundingMode mode_)
{
RoundingMode mode = mode_;
if (mode == null) mode = DEFAULT_ROUND_MODE;
_round_mode = mode;
}
public static void update_round_decimals(int decimals_)
{
int decimals = decimals_;
if (decimals < 0 || decimals > MAX_DIGITS_DECIMAL) decimals = DEFAULT_ROUND_DECIMALS;
_round_decimals = decimals;
}
public static boolean is_ok(double input_, double min_, double max_, double ignore_) { return ((input_ >= min_ || min_ == ignore_) && (input_ <= max_ || max_ == ignore_)); }
public static boolean is_ok(double input_, double min_, double max_) { return (input_ >= min_ && input_ <= max_); }
public static boolean is_ok(long input_, long min_, long max_, long ignore_) { return ((input_ >= min_ || min_ == ignore_) && (input_ <= max_ || max_ == ignore_)); }
public static boolean is_ok(long input_, long min_, long max_) { return (input_ >= min_ && input_ <= max_); }
public static boolean is_ok(int input_, int min_, int max_, int ignore_) { return ((input_ >= min_ || min_ == ignore_) && (input_ <= max_ || max_ == ignore_)); }
public static boolean is_ok(int input_, int min_, int max_) { return (input_ >= min_ && input_ <= max_); }
public static boolean is_ok(Object input_) { return generic.is_number(input_); }
public static boolean are_equivalent(double val1_, double val2_) { return are_equivalent(val1_, val2_, _round_decimals); }
public static boolean are_equivalent(double val1_, double val2_, int round_decimals_) { return (round(val1_, round_decimals_) == round(val2_, round_decimals_)); }
public static double to_number(Object input_)
{
double output = 0.0;
Class<?> type = generic.get_class(input_);
if (generic.are_equal(type, Double.class)) output = (double)input_;
else if (generic.are_equal(type, Integer.class)) output = (double)((int)input_); //!!!
else if (generic.are_equal(type, Long.class)) output = (double)((long)input_); //!!!
else if (generic.are_equal(type, Boolean.class)) output = (double)to_int((boolean)input_);
else if (generic.are_equal(type, String.class)) output = to_decimal((String)input_);
return output;
}
public static Object get_random(Class<?> class_)
{
Object output = null;
if (generic.are_equal(class_, Double.class)) output = get_random_decimal(MIN_DECIMAL, MAX_DECIMAL);
else if (generic.are_equal(class_, Long.class)) output = get_random_long(MIN_LONG, MAX_LONG);
else if (generic.are_equal(class_, Integer.class)) output = get_random_int(MIN_INT, MAX_INT);
return output;
}
public static int get_random_index(int max_i_) { return get_random_int(0, max_i_); }
public static int get_random_int(int min_, int max_)
{
if (min_ > max_) return 0;
if (min_ == max_) return min_;
if (min_ != 0) return (int)get_random_decimal((double)min_, (double)max_);
Random random = new Random();
int output = random.nextInt(max_);
if (output < min_) output = min_;
if (output > max_) output = max_;
return output;
}
public static long get_random_long(long min_, long max_)
{
if (min_ > max_) return 0;
if (min_ == max_) return min_;
return (long)get_random_decimal((double)min_, (double)max_);
}
public static double get_random_decimal(double min_, double max_)
{
if (min_ > max_) return 0.0;
if (min_ == max_) return min_;
double output = (new Random()).nextDouble();
output = (min_ + output * (max_ - min_));
if (output < min_) output = min_;
if (output > max_) output = max_;
return output;
}
public static int get_length(int input_) { return get_length((double)input_); }
public static int get_length(long input_) { return get_length((double)input_); }
public static int get_length(double input_)
{
int length = 1;
double input = Math.abs(input_);
if (input < 10.0) return length;
while (input >= 10.0)
{
input /= 10.0;
length++;
}
return length;
}
public static double to_decimal(String input_) { return strings.to_number_decimal(input_); }
public static long to_long(double input_)
{
double output = Math.floor(input_);
return (long)((output >= (double)MIN_LONG && output <= (double)MAX_LONG) ? output : DEFAULT_LONG);
}
public static long to_long(String input_) { return strings.to_number_long(input_); }
public static int to_int(double input_)
{
double output = Math.floor(input_);
return (int)((output >= (double)MIN_INT && output <= (double)MAX_INT) ? output : DEFAULT_INT);
}
public static int to_int(boolean input_) { return (input_ ? 1 : 0); }
public static int to_int(String input_) { return strings.to_number_int(input_); }
public static boolean to_boolean(int input_) { return (input_ == 1); }
public static int from_boolean(boolean input_) { return (input_ ? 1 : 0); }
public static String to_string_decimal(double input_, boolean to_int_) { return (to_int_ ? to_string_decimal_integer(input_) : Double.toString(input_)); }
public static String to_string_long(long input_) { return Long.toString(input_); }
public static String to_string_int(int input_) { return Integer.toString(input_); }
public static boolean are_similar(double val1_, double val2_) { return are_similar(val1_, val2_, DEFAULT_PERC_SIMILAR); }
public static boolean are_similar(double val1_, double val2_, double perc_) { return (get_perc_generic(val1_, val2_, false, true) <= Math.abs(perc_)); }
public static double get_perc(double val1_, double val2_) { return get_perc(val1_, val2_, false, false); }
public static double get_perc(double val1_, double val2_, boolean round_, boolean out_abs_) { return get_perc_common(val1_, val2_, round_, out_abs_, true); }
public static double get_perc_generic(double val1_, double val2_) { return get_perc_generic(val1_, val2_, false, false); }
public static double get_perc_generic(double val1_, double val2_, boolean round_, boolean out_abs_) { return get_perc_common(val1_, val2_, round_, out_abs_, true); }
public static double get_perc_hist(double new_, double old_) { return get_perc_hist(new_, old_, false, false); }
public static double get_perc_hist(double new_, double old_, boolean round_, boolean out_abs_) { return get_perc_common(new_, old_, round_, out_abs_, false); }
public static double apply_perc(double val_, double perc_) { return apply_perc(val_, perc_, false); }
public static double apply_perc(double val_, double perc_, boolean round_)
{
double output = (val_ * (100.0 + perc_) / 100.0);
if (round_) output = round(output);
return output;
}
public static double round(double val_) { return round(val_, _round_decimals); }
public static double round(double val_, int decimals_) { return round(val_, decimals_, _round_mode); }
static final Class<?>[] populate_all_classes() { return new Class<?>[] { Integer.class, int.class, Long.class, long.class, Double.class, double.class }; }
private static double get_perc_common(double new_, double old_, boolean round_, boolean out_abs_, boolean is_generic_)
{
double output = 0;
if (old_ == 0) return output;
double new2 = new_;
double old2 = old_;
if (is_generic_)
{
if (config.get_numbers_boolean(PERC_REF_LARGER) && (Math.abs(new2) > Math.abs(old2)))
{
new2 = old_;
old2 = new_;
}
}
output = 100 * (new2 - old2) / old2;
if (out_abs_ || is_generic_) output = Math.abs(output);
if (round_) output = round(output);
return output;
}
private static double round(double val_, int decimals_, RoundingMode mode_)
{
if (decimals_ == 0) return val_;
RoundingMode mode = mode_;
if (mode == null) mode = DEFAULT_ROUND_MODE;
int decimals = decimals_;
if (decimals < 0 || decimals > MAX_DECIMALS) decimals = DEFAULT_ROUND_DECIMALS;
return (new BigDecimal(val_)).setScale(decimals, mode_).doubleValue();
}
private static String to_string_decimal_integer(double input_)
{
String output = "";
double input = input_;
int length = get_length(input);
boolean is_negative = (input < 0);
if (length < MAX_DIGITS_LONG) return Long.toString((long)input);
input = Math.abs(input);
while (input >= 1)
{
output = (int)(input % 10) + output;
input /= 10;
}
if (is_negative) output = "-" + output;
return output;
}
}