From 5e48e1c7cb6134bd31e659e8747cbc36f95fc685 Mon Sep 17 00:00:00 2001 From: Sublimis Date: Mon, 9 Nov 2020 14:55:09 +0100 Subject: [PATCH] Update Utils.java Use double-s instead of float-s to minimize rounding errors that can lead to axis labels saying e.g. "9999.999" instead of "10000". There's really no excuse for using float-s here, and probably throughout much of the rest of the code, too. --- .../github/mikephil/charting/utils/Utils.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java index c302673919..dac8677401 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java @@ -352,15 +352,15 @@ public static String formatNumber(float number, int digitCount, boolean separate * @param number * @return */ - public static float roundToNextSignificant(double number) { - if (Double.isInfinite(number) || - Double.isNaN(number) || + public static double roundToNextSignificant(double number) { + if (Double.isInfinite(number) || + Double.isNaN(number) || number == 0.0) return 0; - - final float d = (float) Math.ceil((float) Math.log10(number < 0 ? -number : number)); + + final double d = Math.ceil(Math.log10(number < 0 ? -number : number)); final int pw = 1 - (int) d; - final float magnitude = (float) Math.pow(10, pw); + final double magnitude = Math.pow(10, pw); final long shifted = Math.round(number * magnitude); return shifted / magnitude; } @@ -374,11 +374,11 @@ public static float roundToNextSignificant(double number) { */ public static int getDecimals(float number) { - float i = roundToNextSignificant(number); - - if (Float.isInfinite(i)) + double i = roundToNextSignificant(number); + + if (Double.isInfinite(i)) return 0; - + return (int) Math.ceil(-Math.log10(i)) + 2; }