package com.example.linechartlibrary;

import com.clj.fastble.BleManager;
import kotlin.time.DurationKt;

/* loaded from: classes.dex */
public class FloatUtils {
    public static final int[] POW10 = {1, 10, 100, 1000, BleManager.DEFAULT_SCAN_TIME, 100000, DurationKt.NANOS_IN_MILLIS};

    public static float nextUpF(float f) {
        if (Float.isNaN(f) || f == Float.POSITIVE_INFINITY) {
            return f;
        }
        float f2 = f + 0.0f;
        return Float.intBitsToFloat(Float.floatToRawIntBits(f2) + (f2 >= 0.0f ? 1 : -1));
    }

    public static float nextDownF(float f) {
        if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY) {
            return f;
        }
        if (f == 0.0f) {
            return -1.4E-45f;
        }
        return Float.intBitsToFloat(Float.floatToRawIntBits(f) + (f > 0.0f ? -1 : 1));
    }

    public static double nextUp(double d) {
        if (Double.isNaN(d) || d == Double.POSITIVE_INFINITY) {
            return d;
        }
        double d2 = d + 0.0d;
        return Double.longBitsToDouble(Double.doubleToRawLongBits(d2) + (d2 >= 0.0d ? 1 : -1));
    }

    public static double nextDown(double d) {
        if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY) {
            return d;
        }
        if (d == 0.0d) {
            return -1.401298464324817E-45d;
        }
        return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + (d > 0.0d ? -1 : 1));
    }

    public static boolean almostEqual(float f, float f2, float f3, float f4) {
        float abs = Math.abs(f - f2);
        if (abs <= f3) {
            return true;
        }
        float abs2 = Math.abs(f);
        float abs3 = Math.abs(f2);
        if (abs2 <= abs3) {
            abs2 = abs3;
        }
        return abs <= abs2 * f4;
    }

    public static float roundToOneSignificantFigure(double d) {
        float pow = (float) Math.pow(10.0d, 1 - ((int) Math.ceil((float) Math.log10(d < 0.0d ? -d : d))));
        return ((float) Math.round(d * pow)) / pow;
    }

    public static int formatFloat(char[] cArr, float f, int i, int i2, char c) {
        boolean z;
        int[] iArr = POW10;
        if (i2 >= iArr.length) {
            cArr[i - 1] = '.';
            return 1;
        }
        if (f == 0.0f) {
            cArr[i - 1] = '0';
            return 1;
        }
        int i3 = 0;
        if (f < 0.0f) {
            f = -f;
            z = true;
        } else {
            z = false;
        }
        if (i2 > iArr.length) {
            i2 = iArr.length - 1;
        }
        long round = Math.round(f * iArr[i2]);
        int i4 = i - 1;
        while (true) {
            if (round == 0 && i3 >= i2 + 1) {
                break;
            }
            int i5 = (int) (round % 10);
            round /= 10;
            int i6 = i4 - 1;
            cArr[i4] = (char) (i5 + 48);
            int i7 = i3 + 1;
            if (i7 == i2) {
                i4 -= 2;
                cArr[i6] = c;
                i3 += 2;
            } else {
                i3 = i7;
                i4 = i6;
            }
        }
        if (cArr[i4 + 1] == c) {
            cArr[i4] = '0';
            i3++;
            i4--;
        }
        if (!z) {
            return i3;
        }
        cArr[i4] = '-';
        return i3 + 1;
    }

    public static void computeAutoGeneratedAxisValues(float f, float f2, int i, AxisAutoValues axisAutoValues) {
        double d = f2 - f;
        if (i == 0 || d <= 0.0d) {
            axisAutoValues.values = new float[0];
            axisAutoValues.valuesNumber = 0;
            return;
        }
        double roundToOneSignificantFigure = roundToOneSignificantFigure(d / i);
        double pow = Math.pow(10.0d, (int) Math.log10(roundToOneSignificantFigure));
        if (((int) (roundToOneSignificantFigure / pow)) > 5) {
            roundToOneSignificantFigure = Math.floor(pow * 10.0d);
        }
        double ceil = Math.ceil(f / roundToOneSignificantFigure) * roundToOneSignificantFigure;
        int i2 = 0;
        for (double d2 = ceil; d2 <= nextUp(Math.floor(f2 / roundToOneSignificantFigure) * roundToOneSignificantFigure); d2 += roundToOneSignificantFigure) {
            i2++;
        }
        axisAutoValues.valuesNumber = i2;
        if (axisAutoValues.values.length < i2) {
            axisAutoValues.values = new float[i2];
        }
        for (int i3 = 0; i3 < i2; i3++) {
            axisAutoValues.values[i3] = (float) ceil;
            ceil += roundToOneSignificantFigure;
        }
        if (roundToOneSignificantFigure < 1.0d) {
            axisAutoValues.decimals = (int) Math.ceil(-Math.log10(roundToOneSignificantFigure));
        } else {
            axisAutoValues.decimals = 0;
        }
    }
}
