Merge "[poincare] Improve scientific display mode"

This commit is contained in:
Émilie Feral
2017-03-16 17:37:23 +01:00
committed by Gerrit
3 changed files with 13 additions and 10 deletions

View File

@@ -290,8 +290,8 @@ int Complex::convertFloatToTextPrivate(float f, char * buffer, int numberOfSigni
int dividend = fabsf((float)mantissa);
int quotien = dividend/10;
int digit = dividend - quotien*10;
int minimumNumberOfCharsInMantissa = displayMode == FloatDisplayMode::Scientific ? 3 : 1;
while (digit == 0 && availableCharsForMantissaWithSign > minimumNumberOfCharsInMantissa &&
int minimumNumberOfCharsInMantissa = 1;
while (digit == 0 && availableCharsForMantissaWithoutSign > minimumNumberOfCharsInMantissa &&
(availableCharsForMantissaWithoutSign > exponentInBase10+2 || displayMode == FloatDisplayMode::Scientific)) {
mantissa = mantissa/10;
availableCharsForMantissaWithoutSign--;
@@ -302,13 +302,14 @@ int Complex::convertFloatToTextPrivate(float f, char * buffer, int numberOfSigni
}
// Suppress the decimal marker if no fractional part
if (displayMode == FloatDisplayMode::Decimal && availableCharsForMantissaWithoutSign == exponentInBase10+2) {
if ((displayMode == FloatDisplayMode::Decimal && availableCharsForMantissaWithoutSign == exponentInBase10+2)
|| (displayMode == FloatDisplayMode::Scientific && availableCharsForMantissaWithoutSign == 2)) {
availableCharsForMantissaWithSign--;
}
// Print mantissa
printBase10IntegerWithDecimalMarker(buffer, availableCharsForMantissaWithSign, mantissa, decimalMarkerPosition);
if (displayMode == FloatDisplayMode::Decimal) {
if (displayMode == FloatDisplayMode::Decimal || exponentInBase10 == 0.0f) {
buffer[availableCharsForMantissaWithSign] = 0;
return availableCharsForMantissaWithSign;
}

View File

@@ -120,8 +120,10 @@ number:
| DIGITS DOT DIGITS { $$ = new Poincare::Complex($1.address, $1.length, false, $3.address, $3.length, nullptr, 0, false); }
| DOT DIGITS EE DIGITS { $$ = new Poincare::Complex(nullptr, 0, false, $2.address, $2.length, $4.address, $4.length, false); }
| DIGITS DOT DIGITS EE DIGITS { $$ = new Poincare::Complex($1.address, $1.length, false, $3.address, $3.length, $5.address, $5.length, false); }
| DIGITS EE DIGITS { $$ = new Poincare::Complex($1.address, $1.length, false, nullptr, 0, $3.address, $3.length, false); }
| DOT DIGITS EE MINUS DIGITS { $$ = new Poincare::Complex(nullptr, 0, false, $2.address, $2.length, $5.address, $5.length, true); }
| DIGITS DOT DIGITS EE MINUS DIGITS { $$ = new Poincare::Complex($1.address, $1.length, false, $3.address, $3.length, $6.address, $6.length, true); }
| DIGITS EE MINUS DIGITS { $$ = new Poincare::Complex($1.address, $1.length, false, nullptr, 0, $4.address, $4.length, true); }
symb:
SYMBOL { $$ = new Poincare::Symbol($1); }

View File

@@ -12,10 +12,10 @@ QUIZ_CASE(poincare_complex_to_text) {
char result1[20] = {'1','.','2','3','4','5','6',Ion::Charset::Exponent,'2',0};
assert(strcmp(buffer, result1) == 0);
Complex::convertFloatToText(1.234567891011f,buffer, 14, 7, Expression::FloatDisplayMode::Scientific);
char result2[20] = {'1','.','2','3','4','5','6','8',Ion::Charset::Exponent,'0',0};
char result2[20] = {'1','.','2','3','4','5','6','8',0};
assert(strcmp(buffer, result2) == 0);
Complex::convertFloatToText(2.0f, buffer, 14, 7, Expression::FloatDisplayMode::Scientific);
char result3[20] = {'2','.','0',Ion::Charset::Exponent,'0',0};
char result3[20] = {'2',0};
assert(strcmp(buffer, result3) == 0);
Complex::convertFloatToText(123456789.0f, buffer, 14, 7, Expression::FloatDisplayMode::Scientific);
char result4[20] = {'1','.','2','3','4','5','6','8',Ion::Charset::Exponent,'8',0};
@@ -33,22 +33,22 @@ QUIZ_CASE(poincare_complex_to_text) {
char result8[20] = {'-','1','.','2','3','4','5','6','8',Ion::Charset::Exponent,'-','4',0};
assert(strcmp(buffer, result8) == 0);
Complex::convertFloatToText(0.0f, buffer, 14, 7, Expression::FloatDisplayMode::Scientific);
char result9[20] = {'0','.','0',Ion::Charset::Exponent,'0',0};
char result9[20] = {'0',0};
assert(strcmp(buffer, result9) == 0);
Complex::convertFloatToText(10000000000000000000000000000.0f, buffer, 14, 7, Expression::FloatDisplayMode::Scientific);
char result10[20] = {'1','.','0',Ion::Charset::Exponent,'2','8',0};
char result10[20] = {'1',Ion::Charset::Exponent,'2','8',0};
assert(strcmp(buffer, result10) == 0);
Complex::convertFloatToText(10000000000000000000000000000.0f, buffer, 14, 7, Expression::FloatDisplayMode::Decimal);
assert(strcmp(buffer, result10) == 0);
Complex::convertFloatToText(1000000.0f, buffer, 14, 7, Expression::FloatDisplayMode::Decimal);
assert(strcmp(buffer, "1000000") == 0);
Complex::convertFloatToText(10000000.0f, buffer, 14, 7, Expression::FloatDisplayMode::Decimal);
char result11[20] = {'1','.','0',Ion::Charset::Exponent,'7',0};
char result11[20] = {'1',Ion::Charset::Exponent,'7',0};
assert(strcmp(buffer, result11) == 0);
Complex::convertFloatToText(0.000001f, buffer, 14, 7, Expression::FloatDisplayMode::Decimal);
assert(strcmp(buffer, "0.000001") == 0);
Complex::convertFloatToText(0.0000001f, buffer, 14, 7, Expression::FloatDisplayMode::Decimal);
char result12[20] = {'1','.','0',Ion::Charset::Exponent,'-','7',0};
char result12[20] = {'1',Ion::Charset::Exponent,'-','7',0};
assert(strcmp(buffer, result12) == 0);
char buffer2[6];
Complex::convertFloatToText(123.421f, buffer2, 6, 4, Expression::FloatDisplayMode::Decimal);