310 lines
11 KiB
Java
310 lines
11 KiB
Java
|
package com.google.common.math;
|
||
|
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.primitives.Booleans;
|
||
|
import java.math.BigInteger;
|
||
|
import java.math.RoundingMode;
|
||
|
import java.util.Iterator;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public final class DoubleMath {
|
||
|
static final int MAX_FACTORIAL = 170;
|
||
|
private static final double MAX_INT_AS_DOUBLE = 2.147483647E9d;
|
||
|
private static final double MAX_LONG_AS_DOUBLE_PLUS_ONE = 9.223372036854776E18d;
|
||
|
private static final double MIN_INT_AS_DOUBLE = -2.147483648E9d;
|
||
|
private static final double MIN_LONG_AS_DOUBLE = -9.223372036854776E18d;
|
||
|
private static final double LN_2 = Math.log(2.0d);
|
||
|
static final double[] everySixteenthFactorial = {1.0d, 2.0922789888E13d, 2.631308369336935E35d, 1.2413915592536073E61d, 1.2688693218588417E89d, 7.156945704626381E118d, 9.916779348709496E149d, 1.974506857221074E182d, 3.856204823625804E215d, 5.5502938327393044E249d, 4.7147236359920616E284d};
|
||
|
|
||
|
static double roundIntermediate(double d, RoundingMode roundingMode) {
|
||
|
if (!DoubleUtils.isFinite(d)) {
|
||
|
throw new ArithmeticException("input is infinite or NaN");
|
||
|
}
|
||
|
switch (AnonymousClass1.$SwitchMap$java$math$RoundingMode[roundingMode.ordinal()]) {
|
||
|
case 1:
|
||
|
MathPreconditions.checkRoundingUnnecessary(isMathematicalInteger(d));
|
||
|
return d;
|
||
|
case 2:
|
||
|
return (d >= LN_2 || isMathematicalInteger(d)) ? d : ((long) d) - 1;
|
||
|
case 3:
|
||
|
return (d <= LN_2 || isMathematicalInteger(d)) ? d : ((long) d) + 1;
|
||
|
case 4:
|
||
|
return d;
|
||
|
case 5:
|
||
|
if (isMathematicalInteger(d)) {
|
||
|
return d;
|
||
|
}
|
||
|
return ((long) d) + (d > LN_2 ? 1 : -1);
|
||
|
case 6:
|
||
|
return Math.rint(d);
|
||
|
case 7:
|
||
|
double rint = Math.rint(d);
|
||
|
return Math.abs(d - rint) == 0.5d ? d + Math.copySign(0.5d, d) : rint;
|
||
|
case 8:
|
||
|
double rint2 = Math.rint(d);
|
||
|
return Math.abs(d - rint2) == 0.5d ? d : rint2;
|
||
|
default:
|
||
|
throw new AssertionError();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* renamed from: com.google.common.math.DoubleMath$1, reason: invalid class name */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public static /* synthetic */ class AnonymousClass1 {
|
||
|
static final int[] $SwitchMap$java$math$RoundingMode;
|
||
|
|
||
|
static {
|
||
|
int[] iArr = new int[RoundingMode.values().length];
|
||
|
$SwitchMap$java$math$RoundingMode = iArr;
|
||
|
try {
|
||
|
iArr[RoundingMode.UNNECESSARY.ordinal()] = 1;
|
||
|
} catch (NoSuchFieldError unused) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.FLOOR.ordinal()] = 2;
|
||
|
} catch (NoSuchFieldError unused2) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.CEILING.ordinal()] = 3;
|
||
|
} catch (NoSuchFieldError unused3) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.DOWN.ordinal()] = 4;
|
||
|
} catch (NoSuchFieldError unused4) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.UP.ordinal()] = 5;
|
||
|
} catch (NoSuchFieldError unused5) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.HALF_EVEN.ordinal()] = 6;
|
||
|
} catch (NoSuchFieldError unused6) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.HALF_UP.ordinal()] = 7;
|
||
|
} catch (NoSuchFieldError unused7) {
|
||
|
}
|
||
|
try {
|
||
|
$SwitchMap$java$math$RoundingMode[RoundingMode.HALF_DOWN.ordinal()] = 8;
|
||
|
} catch (NoSuchFieldError unused8) {
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static int roundToInt(double d, RoundingMode roundingMode) {
|
||
|
double roundIntermediate = roundIntermediate(d, roundingMode);
|
||
|
MathPreconditions.checkInRangeForRoundingInputs((roundIntermediate > -2.147483649E9d) & (roundIntermediate < 2.147483648E9d), d, roundingMode);
|
||
|
return (int) roundIntermediate;
|
||
|
}
|
||
|
|
||
|
public static long roundToLong(double d, RoundingMode roundingMode) {
|
||
|
double roundIntermediate = roundIntermediate(d, roundingMode);
|
||
|
MathPreconditions.checkInRangeForRoundingInputs((MIN_LONG_AS_DOUBLE - roundIntermediate < 1.0d) & (roundIntermediate < MAX_LONG_AS_DOUBLE_PLUS_ONE), d, roundingMode);
|
||
|
return (long) roundIntermediate;
|
||
|
}
|
||
|
|
||
|
public static BigInteger roundToBigInteger(double d, RoundingMode roundingMode) {
|
||
|
double roundIntermediate = roundIntermediate(d, roundingMode);
|
||
|
if ((MIN_LONG_AS_DOUBLE - roundIntermediate < 1.0d) & (roundIntermediate < MAX_LONG_AS_DOUBLE_PLUS_ONE)) {
|
||
|
return BigInteger.valueOf((long) roundIntermediate);
|
||
|
}
|
||
|
BigInteger shiftLeft = BigInteger.valueOf(DoubleUtils.getSignificand(roundIntermediate)).shiftLeft(Math.getExponent(roundIntermediate) - 52);
|
||
|
return roundIntermediate < LN_2 ? shiftLeft.negate() : shiftLeft;
|
||
|
}
|
||
|
|
||
|
public static boolean isPowerOfTwo(double d) {
|
||
|
if (d <= LN_2 || !DoubleUtils.isFinite(d)) {
|
||
|
return false;
|
||
|
}
|
||
|
long significand = DoubleUtils.getSignificand(d);
|
||
|
return (significand & (significand - 1)) == 0;
|
||
|
}
|
||
|
|
||
|
public static double log2(double d) {
|
||
|
return Math.log(d) / LN_2;
|
||
|
}
|
||
|
|
||
|
/* JADX WARN: Failed to find 'out' block for switch in B:12:0x0032. Please report as an issue. */
|
||
|
/* JADX WARN: Removed duplicated region for block: B:25:? A[RETURN, SYNTHETIC] */
|
||
|
/*
|
||
|
Code decompiled incorrectly, please refer to instructions dump.
|
||
|
To view partially-correct add '--show-bad-code' argument
|
||
|
*/
|
||
|
public static int log2(double r4, java.math.RoundingMode r6) {
|
||
|
/*
|
||
|
r0 = 0
|
||
|
int r0 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1))
|
||
|
r1 = 0
|
||
|
r2 = 1
|
||
|
if (r0 <= 0) goto L10
|
||
|
boolean r0 = com.google.common.math.DoubleUtils.isFinite(r4)
|
||
|
if (r0 == 0) goto L10
|
||
|
r0 = r2
|
||
|
goto L11
|
||
|
L10:
|
||
|
r0 = r1
|
||
|
L11:
|
||
|
java.lang.String r3 = "x must be positive and finite"
|
||
|
com.google.common.base.Preconditions.checkArgument(r0, r3)
|
||
|
int r0 = java.lang.Math.getExponent(r4)
|
||
|
boolean r3 = com.google.common.math.DoubleUtils.isNormal(r4)
|
||
|
if (r3 != 0) goto L2a
|
||
|
r0 = 4841369599423283200(0x4330000000000000, double:4.503599627370496E15)
|
||
|
double r4 = r4 * r0
|
||
|
int r4 = log2(r4, r6)
|
||
|
int r4 = r4 + (-52)
|
||
|
return r4
|
||
|
L2a:
|
||
|
int[] r3 = com.google.common.math.DoubleMath.AnonymousClass1.$SwitchMap$java$math$RoundingMode
|
||
|
int r6 = r6.ordinal()
|
||
|
r6 = r3[r6]
|
||
|
switch(r6) {
|
||
|
case 1: goto L63;
|
||
|
case 2: goto L6a;
|
||
|
case 3: goto L59;
|
||
|
case 4: goto L4f;
|
||
|
case 5: goto L47;
|
||
|
case 6: goto L3b;
|
||
|
case 7: goto L3b;
|
||
|
case 8: goto L3b;
|
||
|
default: goto L35;
|
||
|
}
|
||
|
L35:
|
||
|
java.lang.AssertionError r4 = new java.lang.AssertionError
|
||
|
r4.<init>()
|
||
|
throw r4
|
||
|
L3b:
|
||
|
double r4 = com.google.common.math.DoubleUtils.scaleNormalize(r4)
|
||
|
double r4 = r4 * r4
|
||
|
r1 = 4611686018427387904(0x4000000000000000, double:2.0)
|
||
|
int r4 = (r4 > r1 ? 1 : (r4 == r1 ? 0 : -1))
|
||
|
if (r4 <= 0) goto L6a
|
||
|
goto L60
|
||
|
L47:
|
||
|
if (r0 < 0) goto L4a
|
||
|
r1 = r2
|
||
|
L4a:
|
||
|
boolean r4 = isPowerOfTwo(r4)
|
||
|
goto L56
|
||
|
L4f:
|
||
|
if (r0 >= 0) goto L52
|
||
|
r1 = r2
|
||
|
L52:
|
||
|
boolean r4 = isPowerOfTwo(r4)
|
||
|
L56:
|
||
|
r4 = r4 ^ r2
|
||
|
r4 = r4 & r1
|
||
|
goto L5e
|
||
|
L59:
|
||
|
boolean r4 = isPowerOfTwo(r4)
|
||
|
r4 = r4 ^ r2
|
||
|
L5e:
|
||
|
if (r4 == 0) goto L6a
|
||
|
L60:
|
||
|
int r0 = r0 + 1
|
||
|
goto L6a
|
||
|
L63:
|
||
|
boolean r4 = isPowerOfTwo(r4)
|
||
|
com.google.common.math.MathPreconditions.checkRoundingUnnecessary(r4)
|
||
|
L6a:
|
||
|
return r0
|
||
|
*/
|
||
|
throw new UnsupportedOperationException("Method not decompiled: com.google.common.math.DoubleMath.log2(double, java.math.RoundingMode):int");
|
||
|
}
|
||
|
|
||
|
public static boolean isMathematicalInteger(double d) {
|
||
|
return DoubleUtils.isFinite(d) && (d == LN_2 || 52 - Long.numberOfTrailingZeros(DoubleUtils.getSignificand(d)) <= Math.getExponent(d));
|
||
|
}
|
||
|
|
||
|
public static double factorial(int i) {
|
||
|
MathPreconditions.checkNonNegative("n", i);
|
||
|
if (i > 170) {
|
||
|
return Double.POSITIVE_INFINITY;
|
||
|
}
|
||
|
double d = 1.0d;
|
||
|
for (int i2 = (i & (-16)) + 1; i2 <= i; i2++) {
|
||
|
d *= i2;
|
||
|
}
|
||
|
return d * everySixteenthFactorial[i >> 4];
|
||
|
}
|
||
|
|
||
|
public static boolean fuzzyEquals(double d, double d2, double d3) {
|
||
|
MathPreconditions.checkNonNegative("tolerance", d3);
|
||
|
return Math.copySign(d - d2, 1.0d) <= d3 || d == d2 || (Double.isNaN(d) && Double.isNaN(d2));
|
||
|
}
|
||
|
|
||
|
public static int fuzzyCompare(double d, double d2, double d3) {
|
||
|
if (fuzzyEquals(d, d2, d3)) {
|
||
|
return 0;
|
||
|
}
|
||
|
if (d < d2) {
|
||
|
return -1;
|
||
|
}
|
||
|
if (d > d2) {
|
||
|
return 1;
|
||
|
}
|
||
|
return Booleans.compare(Double.isNaN(d), Double.isNaN(d2));
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static double mean(double... dArr) {
|
||
|
Preconditions.checkArgument(dArr.length > 0, "Cannot take mean of 0 values");
|
||
|
double checkFinite = checkFinite(dArr[0]);
|
||
|
long j = 1;
|
||
|
for (int i = 1; i < dArr.length; i++) {
|
||
|
checkFinite(dArr[i]);
|
||
|
j++;
|
||
|
checkFinite += (dArr[i] - checkFinite) / j;
|
||
|
}
|
||
|
return checkFinite;
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static double mean(int... iArr) {
|
||
|
Preconditions.checkArgument(iArr.length > 0, "Cannot take mean of 0 values");
|
||
|
long j = 0;
|
||
|
for (int i : iArr) {
|
||
|
j += i;
|
||
|
}
|
||
|
return j / iArr.length;
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static double mean(long... jArr) {
|
||
|
Preconditions.checkArgument(jArr.length > 0, "Cannot take mean of 0 values");
|
||
|
double d = jArr[0];
|
||
|
long j = 1;
|
||
|
for (int i = 1; i < jArr.length; i++) {
|
||
|
j++;
|
||
|
d += (jArr[i] - d) / j;
|
||
|
}
|
||
|
return d;
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static double mean(Iterable<? extends Number> iterable) {
|
||
|
return mean(iterable.iterator());
|
||
|
}
|
||
|
|
||
|
@Deprecated
|
||
|
public static double mean(Iterator<? extends Number> it) {
|
||
|
Preconditions.checkArgument(it.hasNext(), "Cannot take mean of 0 values");
|
||
|
double checkFinite = checkFinite(it.next().doubleValue());
|
||
|
long j = 1;
|
||
|
while (it.hasNext()) {
|
||
|
j++;
|
||
|
checkFinite += (checkFinite(it.next().doubleValue()) - checkFinite) / j;
|
||
|
}
|
||
|
return checkFinite;
|
||
|
}
|
||
|
|
||
|
private static double checkFinite(double d) {
|
||
|
Preconditions.checkArgument(DoubleUtils.isFinite(d));
|
||
|
return d;
|
||
|
}
|
||
|
|
||
|
private DoubleMath() {
|
||
|
}
|
||
|
}
|