mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-19 05:40:38 +01:00
[poincare] Decimal: writeTextInBuffer has to handle cases when the
mantissa is too big to be serialize
This commit is contained in:
committed by
EmilieNumworks
parent
b1f6b31b8c
commit
f9241bd7e6
@@ -125,17 +125,23 @@ int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignif
|
||||
buffer[currentChar] = 0;
|
||||
return currentChar;
|
||||
}
|
||||
char tempBuffer[200];
|
||||
int mantissaLength = m_mantissa.writeTextInBuffer(tempBuffer, 200);
|
||||
if (strcmp(tempBuffer, "undef") == 0) {
|
||||
strlcpy(buffer, tempBuffer, bufferSize);
|
||||
return mantissaLength;
|
||||
}
|
||||
int nbOfDigitsInMantissaWithoutSign = numberOfDigitsInMantissaWithoutSign();
|
||||
int numberOfRequiredDigits = nbOfDigitsInMantissaWithoutSign > m_exponent ? nbOfDigitsInMantissaWithoutSign : m_exponent;
|
||||
numberOfRequiredDigits = m_exponent < 0 ? 1+nbOfDigitsInMantissaWithoutSign-m_exponent : numberOfRequiredDigits;
|
||||
/* Case 0: the number would be too long if we print it as a natural decimal */
|
||||
if (numberOfRequiredDigits > k_maxLength) {
|
||||
if (nbOfDigitsInMantissaWithoutSign == 1) {
|
||||
currentChar +=m_mantissa.writeTextInBuffer(buffer, bufferSize);
|
||||
currentChar += strlcpy(buffer, tempBuffer, bufferSize);
|
||||
} else {
|
||||
currentChar++;
|
||||
if (currentChar >= bufferSize-1) { return bufferSize-1; }
|
||||
currentChar += m_mantissa.writeTextInBuffer(buffer+currentChar, bufferSize-currentChar);
|
||||
currentChar += strlcpy(buffer+currentChar, tempBuffer, bufferSize-currentChar);
|
||||
int decimalMarkerPosition = 1;
|
||||
if (buffer[1] == '-') {
|
||||
decimalMarkerPosition++;
|
||||
@@ -153,6 +159,8 @@ int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignif
|
||||
return currentChar;
|
||||
}
|
||||
/* Case 2: Print a natural decimal number */
|
||||
int deltaCharMantissa = m_exponent < 0 ? -m_exponent+1 : 0;
|
||||
strlcpy(buffer+deltaCharMantissa, tempBuffer, bufferSize-deltaCharMantissa);
|
||||
if (m_mantissa.isNegative()) {
|
||||
buffer[currentChar++] = '-';
|
||||
}
|
||||
@@ -176,7 +184,7 @@ int Decimal::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignif
|
||||
tempChar = buffer[currentChar];
|
||||
tempCharPosition = currentChar;
|
||||
}
|
||||
currentChar += m_mantissa.writeTextInBuffer(buffer+currentChar, bufferSize-currentChar);
|
||||
currentChar += mantissaLength;
|
||||
if (m_mantissa.isNegative()) { // replace the temp char back
|
||||
buffer[tempCharPosition] = tempChar;
|
||||
}
|
||||
|
||||
@@ -164,6 +164,8 @@ QUIZ_CASE(poincare_decimal_to_text) {
|
||||
assert_expression_prints_to(&e14, "9.99999999999995E-7");
|
||||
Decimal e15(0.0000009999999999999995);
|
||||
assert_expression_prints_to(&e15, "0.000001");
|
||||
Decimal e16(0.0000009999999999901200121020102010201201201021099995);
|
||||
assert_expression_prints_to(&e16, "9.9999999999012E-7");
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_complex_to_text) {
|
||||
|
||||
Reference in New Issue
Block a user