[poincare] Fix bug in convert float to text

Change-Id: I2b672cbb356a60a00a9fd11be8ed3787d2387c83
This commit is contained in:
Émilie Feral
2017-05-05 10:37:48 +02:00
parent abaa4cea47
commit 3c3981e0e4
2 changed files with 5 additions and 3 deletions

View File

@@ -284,13 +284,15 @@ int Complex::convertFloatToTextPrivate(float f, char * buffer, int numberOfSigni
* is too big (or too small), mantissa is now inf. We handle this case by
* using logarithm function. */
if (isnan(mantissa) || isinf(mantissa)) {
mantissa = roundf(powf(10.0f, log10f(f)+(float)(availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal)));
mantissa = roundf(powf(10.0f, log10f(fabsf(f))+(float)(availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal)));
mantissa = copysignf(mantissa, f);
}
/* We update the exponent in base 10 (if 0.99999999 was rounded to 1 for
* instance) */
float truncatedMantissa = (int)(f * powf(10, availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal));
if (isinf(truncatedMantissa) || isnan(truncatedMantissa)) {
truncatedMantissa = (int)(powf(10.0f, log10f(f)+(float)(availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal)));
truncatedMantissa = (int)(powf(10.0f, log10f(fabsf(f))+(float)(availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal)));
truncatedMantissa = copysignf(truncatedMantissa, f);
}
if (mantissa != truncatedMantissa) {
float newLogBase10 = mantissa != 0.0f ? log10f(fabsf(mantissa/powf(10, availableCharsForMantissaWithoutSign - 1 - numberOfDigitBeforeDecimal))) : 0.0f;

View File

@@ -52,7 +52,7 @@ QUIZ_CASE(poincare_complex_to_text) {
assert_float_converts_to(10000000.0f, "1E7", Decimal);
assert_float_converts_to(0.000001f, "0.000001", Decimal);
assert_float_converts_to(0.0000001f, "1E-7", Decimal); // FIXME: Explain. Would overflow?
// CRASH!! assert_float_converts_to(-0.00000000000000000000000000000000909090964f, "WHATEVER", Decimal);
assert_float_converts_to(-0.00000000000000000000000000000000909090964f, "-9.090897E-33", Decimal); // FIXME: explain why it is not -9.090964 ?
assert_float_converts_to(123.421f, "123.4", Decimal, 4, 6);
assert_float_converts_to(123.421f, "1.2E2", Decimal, 5, 6);
}