commons.java subversion repository
sventon subversion web client - http://www.sventon.org |
[show recent changes] | |
Rev: 679 - https://secure.bioinfweb.info/Code/svn/commons.java / trunk / main / info.bioinfweb.commons.core / src / info / bioinfweb / commons / Math2.java |
Show File - Math2.java [show properties] |

1 | /* |
2 | * bioinfweb.commons.java - Shared components of bioinfweb projects made available in a Java library |
3 | * Copyright (C) 2008-2011, 2013-2018 Ben Stöver, Sarah Wiechers |
4 | * <http://commons.bioinfweb.info/Java> |
5 | * |
6 | * This file is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This file is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | package info.bioinfweb.commons; |
20 | |
21 | |
22 | import java.awt.Rectangle; |
23 | import java.math.BigDecimal; |
24 | import java.math.BigInteger; |
25 | import java.math.MathContext; |
26 | import java.math.RoundingMode; |
27 | import java.util.regex.Pattern; |
28 | |
29 | |
30 | |
31 | /** |
32 | * Provides static methods implementing mathematical functions not present in {@code java.lang.}{@link Math}. |
33 | * |
34 | * @author Ben Stöver |
35 | */ |
36 | public class Math2 { |
37 | public static final Pattern INT_PATTERN = Pattern.compile("-?\\d+"); |
38 | |
39 | |
40 | public static double log(double a, double base) { |
41 | return Math.log(a) / Math.log(base); |
42 | } |
43 | |
44 | |
45 | public static int intPow(int basis, int exponent) { |
46 | if (exponent < 0) { |
47 | throw new IllegalArgumentException("The exponent must be greater or equal to zero (" + |
48 | exponent + ")."); |
49 | } |
50 | else { |
51 | int result = 1; |
52 | for (int i = 0; i < exponent; i++) { |
53 | result *= basis; |
54 | } |
55 | return result; |
56 | } |
57 | } |
58 | |
59 | |
60 | public static long longPow(long basis, long exponent) { |
61 | if (exponent < 0) { |
62 | throw new IllegalArgumentException("The exponent must be greater or equal to zero (" + |
63 | exponent + ")."); |
64 | } |
65 | else { |
66 | long result = 1; |
67 | for (long i = 0; i < exponent; i++) { |
68 | result *= basis; |
69 | } |
70 | return result; |
71 | } |
72 | } |
73 | |
74 | |
75 | public static int divAbove(int divided, int divisor) { |
76 | int result = divided / divisor; |
77 | if (divided % divisor > 0) { |
78 | result++; |
79 | } |
80 | return result; |
81 | } |
82 | |
83 | |
84 | /** |
85 | * Checks if the specified value is the string representation of a mathematical integer. |
86 | * |
87 | * @param value the string to be tested |
88 | * @return {@code true} if {@code value} is the string representation of an integer or {@code false} otherwise |
89 | */ |
90 | public static boolean isInt(String value) { |
91 | return INT_PATTERN.matcher(value).matches(); |
92 | } |
93 | |
94 | |
95 | /** |
96 | * Checks if the specified value is a mathematical integer. |
97 | * |
98 | * @param value the value to be tested |
99 | * @return {@code true} if {@code value} is an integer or {@code false} otherwise |
100 | */ |
101 | public static boolean isInt(double value) { |
102 | return (value == Math.rint(value)) && !Double.isInfinite(value); |
103 | } |
104 | |
105 | |
106 | /** |
107 | * Checks if the specified value is a mathematical integer. |
108 | * |
109 | * @param value the value to be tested |
110 | * @return {@code true} if {@code value} is an integer or {@code false} otherwise |
111 | */ |
112 | public static boolean isInt(float value) { |
113 | return (value == (float)Math.rint(value)) && !Float.isInfinite(value); |
114 | } |
115 | |
116 | |
117 | /** |
118 | * Returns the whether the given string can be parsed to a {@code double} or a {@code float}. |
119 | * |
120 | * @param value the string to be parsed |
121 | * @return {@code true} if the given string could be parsed, {@code false} otherwise. |
122 | */ |
123 | public static boolean isDecimal(String value) { |
124 | boolean result = true; |
125 | try { |
126 | parseDouble(value); |
127 | } |
128 | catch (NumberFormatException e) { |
129 | result = false; |
130 | } |
131 | return result; |
132 | } |
133 | |
134 | |
135 | /** |
136 | * Returns the minimum of the specified values. |
137 | * <p> |
138 | * It is possible to pass only one value which is directly returned in that case. |
139 | * |
140 | * @param values a list of values |
141 | * @return the minimal value that was passed |
142 | * |
143 | * @throws IllegalArgumentException if an empty array is specified |
144 | */ |
145 | public static int minInt(int... values) { |
146 | if (values.length == 1) { |
147 | return values[0]; |
148 | } |
149 | else if (values.length >= 2) { |
150 | int result = values[0]; |
151 | for (int i = 1; i < values.length; i++) { |
152 | result = Math.min(result, values[i]); |
153 | } |
154 | return result; |
155 | } |
156 | else { |
157 | throw new IllegalArgumentException("This function must have at least two parameters."); |
158 | } |
159 | } |
160 | |
161 | |
162 | /** |
163 | * Returns the minimum of the specified values. |
164 | * <p> |
165 | * It is possible to pass only one value which is directly returned in that case. |
166 | * |
167 | * @param values a list of values |
168 | * @return the minimal value that was passed |
169 | * |
170 | * @throws IllegalArgumentException if an empty array is specified |
171 | */ |
172 | public static long minLong(long... values) { |
173 | if (values.length == 1) { |
174 | return values[0]; |
175 | } |
176 | else if (values.length >= 2) { |
177 | long result = values[0]; |
178 | for (int i = 1; i < values.length; i++) { |
179 | result = Math.min(result, values[i]); |
180 | } |
181 | return result; |
182 | } |
183 | else { |
184 | throw new IllegalArgumentException("This function must have at least two parameters."); |
185 | } |
186 | } |
187 | |
188 | |
189 | /** |
190 | * Returns the minimum of the specified values. |
191 | * <p> |
192 | * It is possible to pass only one value which is directly returned in that case. |
193 | * |
194 | * @param values a list of values |
195 | * @return the minimal value that was passed |
196 | * |
197 | * @throws IllegalArgumentException if an empty array is specified |
198 | */ |
199 | public static float minFloat(float... values) { |
200 | if (values.length == 1) { |
201 | return values[0]; |
202 | } |
203 | else if (values.length >= 2) { |
204 | float result = values[0]; |
205 | for (int i = 1; i < values.length; i++) { |
206 | result = Math.min(result, values[i]); |
207 | } |
208 | return result; |
209 | } |
210 | else { |
211 | throw new IllegalArgumentException("This function must have at least two parameters."); |
212 | } |
213 | } |
214 | |
215 | |
216 | /** |
217 | * Returns the minimum of the specified values. |
218 | * <p> |
219 | * It is possible to pass only one value which is directly returned in that case. |
220 | * |
221 | * @param values a list of values |
222 | * @return the minimal value that was passed |
223 | * |
224 | * @throws IllegalArgumentException if an empty array is specified |
225 | */ |
226 | public static double minDouble(double... values) { |
227 | if (values.length == 1) { |
228 | return values[0]; |
229 | } |
230 | else if (values.length >= 2) { |
231 | double result = values[0]; |
232 | for (int i = 1; i < values.length; i++) { |
233 | result = Math.min(result, values[i]); |
234 | } |
235 | return result; |
236 | } |
237 | else { |
238 | throw new IllegalArgumentException("This function must have at least two parameters."); |
239 | } |
240 | } |
241 | |
242 | |
243 | /** |
244 | * Returns the maximum of the specified values. |
245 | * <p> |
246 | * It is possible to pass only one value which is directly returned in that case. |
247 | * |
248 | * @param values a list of values |
249 | * @return the maximal value that was passed |
250 | * |
251 | * @throws IllegalArgumentException if an empty array is specified |
252 | */ |
253 | public static int maxInt(int... values) { |
254 | if (values.length == 1) { |
255 | return values[0]; |
256 | } |
257 | else if (values.length >= 2) { |
258 | int result = values[0]; |
259 | for (int i = 1; i < values.length; i++) { |
260 | result = Math.max(result, values[i]); |
261 | } |
262 | return result; |
263 | } |
264 | else { |
265 | throw new IllegalArgumentException("This function must have at least two parameters."); |
266 | } |
267 | } |
268 | |
269 | |
270 | /** |
271 | * Returns the maximum of the specified values. |
272 | * <p> |
273 | * It is possible to pass only one value which is directly returned in that case. |
274 | * |
275 | * @param values a list of values |
276 | * @return the maximal value that was passed |
277 | * |
278 | * @throws IllegalArgumentException if an empty array is specified |
279 | */ |
280 | public static long maxLong(long... values) { |
281 | if (values.length == 1) { |
282 | return values[0]; |
283 | } |
284 | else if (values.length >= 2) { |
285 | long result = values[0]; |
286 | for (int i = 1; i < values.length; i++) { |
287 | result = Math.max(result, values[i]); |
288 | } |
289 | return result; |
290 | } |
291 | else { |
292 | throw new IllegalArgumentException("This function must have at least two parameters."); |
293 | } |
294 | } |
295 | |
296 | |
297 | /** |
298 | * Returns the maximum of the specified values. |
299 | * <p> |
300 | * It is possible to pass only one value which is directly returned in that case. |
301 | * |
302 | * @param values a list of values |
303 | * @return the maximal value that was passed |
304 | * |
305 | * @throws IllegalArgumentException if an empty array is specified |
306 | */ |
307 | public static float maxFloat(float... values) { |
308 | if (values.length == 1) { |
309 | return values[0]; |
310 | } |
311 | else if (values.length >= 2) { |
312 | float result = values[0]; |
313 | for (int i = 1; i < values.length; i++) { |
314 | result = Math.max(result, values[i]); |
315 | } |
316 | return result; |
317 | } |
318 | else { |
319 | throw new IllegalArgumentException("This function must have at least two parameters."); |
320 | } |
321 | } |
322 | |
323 | |
324 | /** |
325 | * Returns the maximum of the specified values. |
326 | * <p> |
327 | * It is possible to pass only one value which is directly returned in that case. |
328 | * |
329 | * @param values a list of values |
330 | * @return the maximal value that was passed |
331 | * |
332 | * @throws IllegalArgumentException if an empty array is specified |
333 | */ |
334 | public static double maxDouble(double... values) { |
335 | if (values.length == 1) { |
336 | return values[0]; |
337 | } |
338 | else if (values.length >= 2) { |
339 | double result = values[0]; |
340 | for (int i = 1; i < values.length; i++) { |
341 | result = Math.max(result, values[i]); |
342 | } |
343 | return result; |
344 | } |
345 | else { |
346 | throw new IllegalArgumentException("This function must have at least two parameters."); |
347 | } |
348 | } |
349 | |
350 | |
351 | /** |
352 | * Checks if the specified value is greater or equal to the specified minimum and |
353 | * lower or equal to the specified maximum. |
354 | * |
355 | * @param value the value to be tested |
356 | * @param min the minimum value |
357 | * @param max the maximum value |
358 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
359 | */ |
360 | public static boolean isBetween(byte value, byte min, byte max) { |
361 | return ((value >= min) && (value <= max)); |
362 | } |
363 | |
364 | |
365 | /** |
366 | * Checks if the specified value is greater or equal to the specified minimum and |
367 | * lower or equal to the specified maximum. |
368 | * |
369 | * @param value the value to be tested |
370 | * @param min the minimum value |
371 | * @param max the maximum value |
372 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
373 | */ |
374 | public static boolean isBetween(int value, int min, int max) { |
375 | return ((value >= min) && (value <= max)); |
376 | } |
377 | |
378 | |
379 | /** |
380 | * Checks if the specified value is greater or equal to the specified minimum and |
381 | * lower or equal to the specified maximum. |
382 | * |
383 | * @param value the value to be tested |
384 | * @param min the minimum value |
385 | * @param max the maximum value |
386 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
387 | */ |
388 | public static boolean isBetween(long value, long min, long max) { |
389 | return ((value >= min) && (value <= max)); |
390 | } |
391 | |
392 | |
393 | /** |
394 | * Checks if the specified value is greater or equal to the specified minimum and |
395 | * lower or equal to the specified maximum. |
396 | * |
397 | * @param value the value to be tested |
398 | * @param min the minimum value |
399 | * @param max the maximum value |
400 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
401 | */ |
402 | public static boolean isBetween(float value, float min, float max) { |
403 | return ((value >= min) && (value <= max)); |
404 | } |
405 | |
406 | |
407 | /** |
408 | * Checks if the specified value is greater or equal to the specified minimum and |
409 | * lower or equal to the specified maximum. |
410 | * |
411 | * @param value the value to be tested |
412 | * @param min the minimum value |
413 | * @param max the maximum value |
414 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
415 | */ |
416 | public static boolean isBetween(double value, double min, double max) { |
417 | return ((value >= min) && (value <= max)); |
418 | } |
419 | |
420 | |
421 | /** |
422 | * Checks if the specified value is greater then the specified minimum and |
423 | * lower than the specified maximum. If value is equal to {@code min} or {@code max} |
424 | * this method will return false. |
425 | * |
426 | * @param value the value to be tested |
427 | * @param min the minimum value |
428 | * @param max the maximum value |
429 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
430 | */ |
431 | public static boolean isBetweenNE(byte value, byte min, byte max) { |
432 | return ((value > min) && (value < max)); |
433 | } |
434 | |
435 | |
436 | /** |
437 | * Checks if the specified value is greater then the specified minimum and |
438 | * lower than the specified maximum. If value is equal to {@code min} or {@code max} |
439 | * this method will return false. |
440 | * |
441 | * @param value the value to be tested |
442 | * @param min the minimum value |
443 | * @param max the maximum value |
444 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
445 | */ |
446 | public static boolean isBetweenNE(int value, int min, int max) { |
447 | return ((value > min) && (value < max)); |
448 | } |
449 | |
450 | |
451 | /** |
452 | * Checks if the specified value is greater then the specified minimum and |
453 | * lower than the specified maximum. If value is equal to {@code min} or {@code max} |
454 | * this method will return false. |
455 | * |
456 | * @param value the value to be tested |
457 | * @param min the minimum value |
458 | * @param max the maximum value |
459 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
460 | */ |
461 | public static boolean isBetweenNE(long value, long min, long max) { |
462 | return ((value > min) && (value < max)); |
463 | } |
464 | |
465 | |
466 | /** |
467 | * Checks if the specified value is greater then the specified minimum and |
468 | * lower than the specified maximum. If value is equal to {@code min} or {@code max} |
469 | * this method will return false. |
470 | * |
471 | * @param value the value to be tested |
472 | * @param min the minimum value |
473 | * @param max the maximum value |
474 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
475 | */ |
476 | public static boolean isBetweenNE(float value, float min, float max) { |
477 | return ((value > min) && (value < max)); |
478 | } |
479 | |
480 | |
481 | /** |
482 | * Checks if the specified value is greater then the specified minimum and |
483 | * lower than the specified maximum. If value is equal to {@code min} or {@code max} |
484 | * this method will return false. |
485 | * |
486 | * @param value the value to be tested |
487 | * @param min the minimum value |
488 | * @param max the maximum value |
489 | * @return {@code true} if {@code value} if between {@code min} and {@code max}, {@code false} otherwise |
490 | */ |
491 | public static boolean isBetweenNE(double value, double min, double max) { |
492 | return ((value > min) && (value < max)); |
493 | } |
494 | |
495 | |
496 | /** |
497 | * Returns the specified value if it is between the specified minimum and maximum. If {@code value} |
498 | * is lower than {@code min}, {@code min} is returned, if it is greater than {@code max}, {@code max} |
499 | * is returned. |
500 | * |
501 | * @param value the value to be moved into the specified interval |
502 | * @param min the lower border of the interval |
503 | * @param max the upper border of the interval |
504 | * @return a value equal to {@code Math.min(max, Math.max(min, value))} |
505 | */ |
506 | public static byte moveBetween(byte value, byte min, byte max) { |
507 | return (byte)Math.min(max, Math.max(min, value)); |
508 | } |
509 | |
510 | |
511 | /** |
512 | * Returns the specified value if it is between the specified minimum and maximum. If {@code value} |
513 | * is lower than {@code min}, {@code min} is returned, if it is greater than {@code max}, {@code max} |
514 | * is returned. |
515 | * |
516 | * @param value the value to be moved into the specified interval |
517 | * @param min the lower border of the interval |
518 | * @param max the upper border of the interval |
519 | * @return a value equal to {@code Math.min(max, Math.max(min, value))} |
520 | */ |
521 | public static int moveBetween(int value, int min, int max) { |
522 | return Math.min(max, Math.max(min, value)); |
523 | } |
524 | |
525 | |
526 | /** |
527 | * Returns the specified value if it is between the specified minimum and maximum. If {@code value} |
528 | * is lower than {@code min}, {@code min} is returned, if it is greater than {@code max}, {@code max} |
529 | * is returned. |
530 | * |
531 | * @param value the value to be moved into the specified interval |
532 | * @param min the lower border of the interval |
533 | * @param max the upper border of the interval |
534 | * @return a value equal to {@code Math.min(max, Math.max(min, value))} |
535 | */ |
536 | public static long moveBetween(long value, long min, long max) { |
537 | return Math.min(max, Math.max(min, value)); |
538 | } |
539 | |
540 | |
541 | /** |
542 | * Returns the specified value if it is between the specified minimum and maximum. If {@code value} |
543 | * is lower than {@code min}, {@code min} is returned, if it is greater than {@code max}, {@code max} |
544 | * is returned. |
545 | * |
546 | * @param value the value to be moved into the specified interval |
547 | * @param min the lower border of the interval |
548 | * @param max the upper border of the interval |
549 | * @return a value equal to {@code Math.min(max, Math.max(min, value))} |
550 | */ |
551 | public static float moveBetween(float value, float min, float max) { |
552 | return Math.min(max, Math.max(min, value)); |
553 | } |
554 | |
555 | |
556 | /** |
557 | * Returns the specified value if it is between the specified minimum and maximum. If {@code value} |
558 | * is lower than {@code min}, {@code min} is returned, if it is greater than {@code max}, {@code max} |
559 | * is returned. |
560 | * |
561 | * @param value the value to be moved into the specified interval |
562 | * @param min the lower border of the interval |
563 | * @param max the upper border of the interval |
564 | * @return a value equal to {@code Math.min(max, Math.max(min, value))} |
565 | */ |
566 | public static double moveBetween(double value, double min, double max) { |
567 | return Math.min(max, Math.max(min, value)); |
568 | } |
569 | |
570 | |
571 | /** |
572 | * Checks whether the two specified intervals have an overlap. Intervals that touch each other are |
573 | * not considered as overlapping. |
574 | * <p> |
575 | * {@code min1} must be lower or equal than {@code max1} and {@code min2} must be lower or equal to {@code max2}. |
576 | * This method does not check whether this conditions are fulfilled. |
577 | * |
578 | * @param min1 the lower border of the first interval |
579 | * @param max1 the upper border of the first interval |
580 | * @param min2 the lower border of the second interval |
581 | * @param max2 the upper border of the second interval |
582 | * @return {@code true} if the two intervals overlap, {@code false} otherwise |
583 | */ |
584 | public static boolean overlaps(byte min1, byte max1, byte min2, byte max2) { |
585 | return isBetween(min1, min2, max2) || isBetween(max1, min2, max2) || isBetween(min2, min1, max1) || isBetween(max2, min1, max1); |
586 | } |
587 | |
588 | |
589 | /** |
590 | * Checks whether the two specified intervals have an overlap. Intervals that touch each other are |
591 | * not considered as overlapping. |
592 | * <p> |
593 | * {@code min1} must be lower or equal than {@code max1} and {@code min2} must be lower or equal to {@code max2}. |
594 | * This method does not check whether this conditions are fulfilled. |
595 | * |
596 | * @param min1 the lower border of the first interval |
597 | * @param max1 the upper border of the first interval |
598 | * @param min2 the lower border of the second interval |
599 | * @param max2 the upper border of the second interval |
600 | * @return {@code true} if the two intervals overlap, {@code false} otherwise |
601 | */ |
602 | public static boolean overlaps(int min1, int max1, int min2, int max2) { |
603 | return isBetween(min1, min2, max2) || isBetween(max1, min2, max2) || isBetween(min2, min1, max1) || isBetween(max2, min1, max1); |
604 | } |
605 | |
606 | |
607 | /** |
608 | * Checks whether the two specified intervals have an overlap. Intervals that touch each other are |
609 | * not considered as overlapping. |
610 | * <p> |
611 | * {@code min1} must be lower or equal than {@code max1} and {@code min2} must be lower or equal to {@code max2}. |
612 | * This method does not check whether this conditions are fulfilled. |
613 | * |
614 | * @param min1 the lower border of the first interval |
615 | * @param max1 the upper border of the first interval |
616 | * @param min2 the lower border of the second interval |
617 | * @param max2 the upper border of the second interval |
618 | * @return {@code true} if the two intervals overlap, {@code false} otherwise |
619 | */ |
620 | public static boolean overlaps(long min1, long max1, long min2, long max2) { |
621 | return isBetween(min1, min2, max2) || isBetween(max1, min2, max2) || isBetween(min2, min1, max1) || isBetween(max2, min1, max1); |
622 | } |
623 | |
624 | |
625 | /** |
626 | * Checks whether the two specified intervals have an overlap. If the upper border of one interval is |
627 | * equal to the lower border of another theses intervals are considered as overlapping. |
628 | * <p> |
629 | * {@code min1} must be lower or equal than {@code max1} and {@code min2} must be lower or equal to {@code max2}. |
630 | * This method does not check whether this conditions are fulfilled. |
631 | * |
632 | * @param min1 the lower border of the first interval |
633 | * @param max1 the upper border of the first interval |
634 | * @param min2 the lower border of the second interval |
635 | * @param max2 the upper border of the second interval |
636 | * @return {@code true} if the two intervals overlap, {@code false} otherwise |
637 | */ |
638 | public static boolean overlaps(float min1, float max1, float min2, float max2) { |
639 | return isBetween(min1, min2, max2) || isBetween(max1, min2, max2) || isBetween(min2, min1, max1) || isBetween(max2, min1, max1); |
640 | } |
641 | |
642 | |
643 | /** |
644 | * Checks whether the two specified intervals have an overlap. If the upper border of one interval is |
645 | * equal to the lower border of another theses intervals are considered as overlapping. |
646 | * <p> |
647 | * {@code min1} must be lower or equal than {@code max1} and {@code min2} must be lower or equal to {@code max2}. |
648 | * This method does not check whether this conditions are fulfilled. |
649 | * |
650 | * @param min1 the lower border of the first interval |
651 | * @param max1 the upper border of the first interval |
652 | * @param min2 the lower border of the second interval |
653 | * @param max2 the upper border of the second interval |
654 | * @return {@code true} if the two intervals overlap, {@code false} otherwise |
655 | */ |
656 | public static boolean overlaps(double min1, double max1, double min2, double max2) { |
657 | return isBetween(min1, min2, max2) || isBetween(max1, min2, max2) || isBetween(min2, min1, max1) || isBetween(max2, min1, max1); |
658 | } |
659 | |
660 | |
661 | public static boolean overlapsNE(byte min1, byte max1, byte min2, byte max2) { |
662 | return ((min1 >= min2) && (min1 < max2)) || ((max1 > min2) && (max1 <= max2)) || |
663 | ((min2 >= min1) && (min2 < max1)) || ((max2 > min1) && (max2 <= max1)); |
664 | } |
665 | |
666 | |
667 | public static boolean overlapsNE(int min1, int max1, int min2, int max2) { |
668 | return ((min1 >= min2) && (min1 < max2)) || ((max1 > min2) && (max1 <= max2)) || |
669 | ((min2 >= min1) && (min2 < max1)) || ((max2 > min1) && (max2 <= max1)); |
670 | } |
671 | |
672 | |
673 | public static boolean overlapsNE(long min1, long max1, long min2, long max2) { |
674 | return ((min1 >= min2) && (min1 < max2)) || ((max1 > min2) && (max1 <= max2)) || |
675 | ((min2 >= min1) && (min2 < max1)) || ((max2 > min1) && (max2 <= max1)); |
676 | } |
677 | |
678 | |
679 | public static boolean overlapsNE(float min1, float max1, float min2, float max2) { |
680 | return isBetweenNE(min1, min2, max2) || isBetweenNE(max1, min2, max2) || |
681 | isBetweenNE(min2, min1, max1) || isBetweenNE(max2, min1, max1); |
682 | } |
683 | |
684 | |
685 | public static boolean overlapsNE(double min1, double max1, double min2, double max2) { |
686 | return ((min1 >= min2) && (min1 < max2)) || ((max1 > min2) && (max1 <= max2)) || |
687 | ((min2 >= min1) && (min2 < max1)) || ((max2 > min1) && (max2 <= max1)); |
688 | } |
689 | |
690 | |
691 | /** |
692 | * Checks if one rectangle is completely contained inside the other. |
693 | * |
694 | * @param containedRect the rectangle that shall be tested to be contained in {@code containingRect} |
695 | * @param containingRect the rectangle that shall be tested to contain {@code containedRect} |
696 | * @return {@code true} if the one rectangle is contained in the other, {@code false} otherwise |
697 | */ |
698 | public static boolean containsRect(Rectangle containedRect, Rectangle containingRect) { |
699 | int dx = containedRect.x - containingRect.x; |
700 | int dy = containedRect.y - containingRect.y; |
701 | return (dx >= 0) && (containedRect.width <= containingRect.width - dx) && |
702 | (dy >= 0) && (containedRect.height <= containingRect.height - dy); |
703 | } |
704 | |
705 | |
706 | /** |
707 | * Returns the next integer value that is greater or equal to the specified value. |
708 | * |
709 | * @param value the floating point value to be converted |
710 | * @return the next {@code long} >= {@code value} |
711 | */ |
712 | public static long roundUp(double value) { |
713 | long result = (long)value; |
714 | if ((double)result < value) { |
715 | result++; |
716 | } |
717 | return result; |
718 | } |
719 | |
720 | |
721 | /** |
722 | * Returns the next integer value that is greater or equal to the specified value. |
723 | * |
724 | * @param value the floating point value to be converted |
725 | * @return the next {@code int} >= {@code value} |
726 | */ |
727 | public static int roundUp(float value) { |
728 | int result = (int)value; |
729 | if ((float)result < value) { |
730 | result++; |
731 | } |
732 | return result; |
733 | } |
734 | |
735 | |
736 | public static BigDecimal roundBigDecimal(BigDecimal value, RoundingMode roundingMode) { |
737 | return value.round(new MathContext(0, roundingMode)); |
738 | } |
739 | |
740 | |
741 | public static BigDecimal floorBigDecimal(BigDecimal value) { |
742 | return roundBigDecimal(value, RoundingMode.FLOOR); |
743 | } |
744 | |
745 | |
746 | public static BigInteger roundBigDecimalToBigInteger(BigDecimal value, RoundingMode roundingMode) { |
747 | return new BigInteger(roundBigDecimal(value, roundingMode).toString()); |
748 | } |
749 | |
750 | |
751 | public static BigInteger floorBigDecimalToBigInteger(BigDecimal value) { |
752 | return new BigInteger(roundBigDecimal(value, RoundingMode.FLOOR).toString()); |
753 | } |
754 | |
755 | |
756 | /** |
757 | * Rounds a double value at the given significant digit. |
758 | * <p> |
759 | * <b>Examples:</b> |
760 | * <ul> |
761 | * <li><code>roundSignificantDigit(12.4, 0)</code> would return <i>12</i></li> |
762 | * <li><code>roundSignificantDigit(12.4, 1)</code> would return <i>10</i></li> |
763 | * <li><code>roundSignificantDigit(0.03462, -3)</code> would return <i>0.035</i></li> |
764 | * </ul> |
765 | * |
766 | * @param value the value to be rounded |
767 | * @param digit the decimal power to be rounded to |
768 | * @return the rounded value |
769 | */ |
770 | public static double roundSignificantDigit(double value, int digit) { |
771 | double digitValue = Math.pow(10, digit); |
772 | double rest = value % digitValue; |
773 | return value = value - rest + Math.round(rest / digitValue) * digitValue; |
774 | } |
775 | |
776 | |
777 | /** |
778 | * Rounds a float value at the given significant digit. |
779 | * <p> |
780 | * <b>Examples:</b> |
781 | * <ul> |
782 | * <li><code>roundSignificantDigit(12.4, 0)</code> would return <i>12</i></li> |
783 | * <li><code>roundSignificantDigit(12.4, 1)</code> would return <i>10</i></li> |
784 | * <li><code>roundSignificantDigit(0.03462, -3)</code> would return <i>0.035</i></li> |
785 | * </ul> |
786 | * |
787 | * @param value the value to be rounded |
788 | * @param digit the decimal power to be rounded to |
789 | * @return the rounded value |
790 | */ |
791 | public static float roundSignificantDigit(float value, int digit) { |
792 | float digitValue = (float)Math.pow(10, digit); |
793 | float rest = value % digitValue; |
794 | return value = value - rest + Math.round(rest / digitValue) * digitValue; |
795 | } |
796 | |
797 | |
798 | /** |
799 | * Rounds a double value at the given first significant digit. |
800 | * <p> |
801 | * <b>Examples:</b> |
802 | * <ul> |
803 | * <li><code>roundSignificantDigit(79.0)</code> would return <i>80.0</i></li> |
804 | * <li><code>roundSignificantDigit(12.0)</code> would return <i>10.0</i></li> |
805 | * <li><code>roundSignificantDigit(0.03462)</code> would return <i>0.03</i></li> |
806 | * </ul> |
807 | * Calling this method is equivalent to |
808 | * <code>roundSignificantDigit(value, (int)Math.floor(Math.log10(value)))</code>. |
809 | * |
810 | * @param value the value to be rounded |
811 | * @return the rounded value |
812 | */ |
813 | public static double roundFirstSignificantDigit(double value) { |
814 | return roundSignificantDigit(value, (int)Math.floor(Math.log10(value))); |
815 | } |
816 | |
817 | |
818 | /** |
819 | * Rounds a float value at the given first significant digit. |
820 | * <p> |
821 | * <b>Examples:</b> |
822 | * <ul> |
823 | * <li><code>roundSignificantDigit(79.0)</code> would return <i>80.0</i></li> |
824 | * <li><code>roundSignificantDigit(12.0)</code> would return <i>10.0</i></li> |
825 | * <li><code>roundSignificantDigit(0.03462)</code> would return <i>0.03</i></li> |
826 | * </ul> |
827 | * Calling this method is equivalent to |
828 | * <code>roundSignificantDigit(value, (int)Math.floor(Math.log10(value)))</code>. |
829 | * |
830 | * @param value the value to be rounded |
831 | * @return the rounded value |
832 | */ |
833 | public static float roundFirstSignificantDigit(float value) { |
834 | return roundSignificantDigit(value, (int)Math.floor(Math.log10(value))); |
835 | } |
836 | |
837 | |
838 | /** |
839 | * Parses a float value which can have either "," or "." as its decimal separator. The |
840 | * string must not contain thousand separators. |
841 | * @param string the string to parse |
842 | * @return the parsed float value |
843 | */ |
844 | public static float parseFloat(String string) { |
845 | return Float.parseFloat(string.replace(',', '.')); |
846 | } |
847 | |
848 | |
849 | /** |
850 | * Parses a double value which can have either "," or "." as its decimal separator. The |
851 | * string must not contain thousand separators. (Distinguishing between "1,236" as 1236 in English and |
852 | * "1,236" as 1.236 in German is not possible.) |
853 | * |
854 | * @param string the string to parse |
855 | * @return the parsed double value |
856 | * @throws NumberFormatException if the specified string does not represent a valid decimal value |
857 | */ |
858 | public static double parseDouble(String string) { |
859 | return Double.parseDouble(string.replace(',', '.')); |
860 | } |
861 | |
862 | |
863 | public static String decimalToString(double decimal, int decimalPlaceCount) { |
864 | if (decimalPlaceCount > 0) { |
865 | decimal *= Math.pow(10, decimalPlaceCount); |
866 | String result = "" + (long)Math.round(decimal); |
867 | if (result.equals("0")) { |
868 | result = "0."; |
869 | for (int i = 1; i <= decimalPlaceCount; i++) { |
870 | result += "0"; |
871 | } |
872 | return result; |
873 | } |
874 | else { |
875 | int separatorPos = result.length() - decimalPlaceCount; |
876 | if (separatorPos == 0) { |
877 | return "0." + result; |
878 | } |
879 | else { |
880 | return result.substring(0, separatorPos) + "." + result.substring(separatorPos, result.length()); |
881 | } |
882 | } |
883 | } |
884 | else { |
885 | return "" + (long)Math.round(decimal); |
886 | } |
887 | } |
888 | |
889 | |
890 | /** |
891 | * Returns the sum of all integers from 1 to the specified value. |
892 | * |
893 | * @param n the value to sum up to (must be greater or equal to zero) |
894 | * @throws ArithmeticException if {@code n} is smaller than zero |
895 | */ |
896 | public static int sum1ToN(int n) { |
897 | if (n < 0) { |
898 | throw new ArithmeticException("n must be greater of equal to zero (" + n + ")"); |
899 | } |
900 | else { |
901 | return (n * (n + 1)) / 2; |
902 | } |
903 | } |
904 | } |
sventon 2.5.1