From b140c768bb9fb22ff642264b25ffad1d4a3937ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 29 Mar 2017 10:30:51 +0200 Subject: [PATCH] [poincare] Fix bug in convert float to text Change-Id: Iee47a093ffc6c3829183d16cf1224abf876bc087 --- poincare/src/complex.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/poincare/src/complex.cpp b/poincare/src/complex.cpp index 19a573f9c..3a92f5ece 100644 --- a/poincare/src/complex.cpp +++ b/poincare/src/complex.cpp @@ -252,11 +252,10 @@ int Complex::convertFloatToTextPrivate(float f, char * buffer, int numberOfSigni } float logBase10 = f != 0.0f ? log10f(fabsf(f)) : 0; - int exponentInBase10 = logBase10; - if ((int)f == 0 && logBase10 != exponentInBase10) { - /* For floats < 0, the exponent in base 10 is the inferior integer part of - * log10(float). We thus decrement the exponent for float < 0 whose exponent - * is not an integer. */ + int exponentInBase10 = floorf(logBase10); + /* Correct the exponent in base 10: sometines the exact log10 of f is 6.999999 + * but is stored as 7 in hardware. We catch these cases here. */ + if (f != 0.0f && logBase10 == (int)logBase10 && fabsf(f) < powf(10.0f, logBase10)) { exponentInBase10--; } @@ -281,11 +280,16 @@ int Complex::convertFloatToTextPrivate(float f, char * buffer, int numberOfSigni int numberOfDigitBeforeDecimal = exponentInBase10 >= 0 || displayMode == FloatDisplayMode::Scientific ? exponentInBase10 + 1 : 1; int mantissa = roundf(f * powf(10, availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal)); + /* We update the exponent in base 10 (if 0.99999999 was rounded to 1 for + * instance) */ + if (mantissa != (int)(f * powf(10, availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal))) { + float newLogBase10 = mantissa != 0.0f ? log10f(fabsf(mantissa/powf(10, availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal))) : 0; + exponentInBase10 = floorf(newLogBase10); + } // Correct the number of digits in mantissa after rounding int mantissaExponentInBase10 = exponentInBase10 > 0 || displayMode == FloatDisplayMode::Scientific ? availableCharsForMantissaWithoutSign - 1 : availableCharsForMantissaWithoutSign + exponentInBase10; if ((int)(mantissa * powf(10, - mantissaExponentInBase10)) > 0) { mantissa = mantissa/10; - exponentInBase10++; } int numberOfCharExponent = exponentInBase10 != 0 ? log10f(fabsf((float)exponentInBase10)) + 1 : 1;