Input-checking inverse cosine. Clamps the input to the domain of acos (i.e. [-1..1]) before evaluating, instead of returning a silent NaN like java.lang.Math.acos. Useful when there's a fear of rounding errors pushing the arg beyond 1 or -1.
(double cos_theta)
| 33 | * fear of rounding errors pushing the arg beyond 1 or -1. |
| 34 | */ |
| 35 | public static final double acos(double cos_theta) { |
| 36 | |
| 37 | if (acos_lookup == null) { |
| 38 | acos_lookup = new double[1000]; |
| 39 | for (int i = 0; i < acos_lookup.length; i++) |
| 40 | acos_lookup[i] = Math.acos(clamp(-1.0 + 2 * i / (acos_lookup.length - 1f), -1, 1)); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | cos_theta = clamp(cos_theta, -1, 1); |
| 45 | |
| 46 | float indx = (float) ((acos_lookup.length - 1f) * (cos_theta + 1) / 2); |
| 47 | int left = (int) indx; |
| 48 | int right = (int) indx + 1; |
| 49 | float alpha = indx - left; |
| 50 | |
| 51 | left = left < 0 ? 0 : (left > acos_lookup.length - 1 ? (acos_lookup.length - 1) : left); |
| 52 | right = right < 0 ? 0 : (right > acos_lookup.length - 1 ? (acos_lookup.length - 1) : right); |
| 53 | |
| 54 | return acos_lookup[left] * (1 - alpha) + alpha * acos_lookup[right]; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Input-checking inverse sine. Clamps the input to the domain of acos [-1..1] before evaluating, instead of returning a silent NaN like java.lang.Math.asin. Useful when there's a fear of |
no test coverage detected