-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathDoubleUtils.java
More file actions
62 lines (50 loc) · 2.48 KB
/
Copy pathDoubleUtils.java
File metadata and controls
62 lines (50 loc) · 2.48 KB
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
package util;
import java.util.function.DoublePredicate;
public class DoubleUtils {
public static double getFirstTrue(DoublePredicate monotonicRisingPredicate, double lowerBound, double upperBound) {
if (!Double.isFinite(lowerBound) || !Double.isFinite(upperBound)) {
throw new IllegalArgumentException("Lower and upper bound must be finite!");
}
if (lowerBound > upperBound) {
throw new IllegalArgumentException("Lower bound must not be greater than upper bound!!");
}
if (monotonicRisingPredicate.test(lowerBound)) {
throw new IllegalArgumentException("Lower bound must not meet predicate!");
}
if (!monotonicRisingPredicate.test(upperBound)) {
throw new IllegalArgumentException("Higher bound must meet predicate!");
}
double ret = computeFirstTrue(monotonicRisingPredicate, lowerBound, upperBound);
if (!monotonicRisingPredicate.test(ret) || monotonicRisingPredicate.test(Math.nextDown(ret))) {
ret = computeFirstTrue(monotonicRisingPredicate, lowerBound, upperBound); //For debugging
throw new AssertionError("computeFirstTrue is implemented incorrectly!");
}
return ret;
}
public static double getLastTrue(DoublePredicate monotonicFallingPredicate, double lowerBound, double upperBound) {
return Math.nextDown(getFirstTrue(b -> !monotonicFallingPredicate.test(b), lowerBound, upperBound));
}
public static double computeFirstTrue(DoublePredicate monotonicRisingPredicate, double lowerBound, double upperBound) {
//predicate always holds for high, never holds for low
while (lowerBound < upperBound) {
if (Math.nextUp(lowerBound) == upperBound) {
return upperBound;
}
double mid = (lowerBound + upperBound) / 2.0;
if (mid <= lowerBound || mid >= upperBound) {
//In case of precision issues, use another way of computing mid
mid = lowerBound + (upperBound - lowerBound) / 2.0;
if (mid <= lowerBound || mid >= upperBound) {
//In case of more precision issues, just use anything between high and low
mid = Math.nextUp(lowerBound);
}
}
if (monotonicRisingPredicate.test(mid)) {
upperBound = mid;
} else {
lowerBound = mid;
}
}
return upperBound;
}
}