mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-19 13:50:28 +01:00
[poincare] Fix Decimal(double) constructor
This commit is contained in:
committed by
LeaNumworks
parent
1bfbc7a5ef
commit
2bf62f2304
@@ -116,8 +116,8 @@ int DecimalNode::convertToText(char * buffer, int bufferSize, Preferences::Print
|
||||
m = Integer::Division(m, Integer(10)).quotient;
|
||||
}
|
||||
}
|
||||
removeZeroAtTheEnd(&m);
|
||||
}
|
||||
removeZeroAtTheEnd(&m);
|
||||
if (m_negative) {
|
||||
buffer[currentChar++] = '-';
|
||||
if (currentChar >= bufferSize-1) { return bufferSize-1; }
|
||||
@@ -283,9 +283,19 @@ template <typename T>
|
||||
Decimal::Decimal(T f) : Number() {
|
||||
assert(!std::isnan(f) && !std::isinf(f));
|
||||
int exp = IEEE754<T>::exponentBase10(f);
|
||||
int64_t mantissaf = std::round((double)f * std::pow((double)10.0, (double)(-exp+PrintFloat::k_numberOfStoredSignificantDigits+1)));
|
||||
Integer m(mantissaf);
|
||||
new (this) Decimal(Integer(mantissaf), exp);
|
||||
/* mantissa = f*10^(-exponent+k_numberOfStoredSignificantDigits+1). We compute
|
||||
* this operations in 2 steps as
|
||||
* 10^(-exponent+k_numberOfStoredSignificantDigits+1) can be infinity.*/
|
||||
double mantissaf = f * std::pow(10.0, (double)(-exp));
|
||||
mantissaf = mantissaf * std::pow((double)10.0, (double)(PrintFloat::k_numberOfStoredSignificantDigits-1));
|
||||
/* If m > 99999999999999.5, the mantissa stored will be 1 (as we keep only
|
||||
* 14 significative numbers from double. In that case, the exponent must be
|
||||
* increment as well. */
|
||||
static double biggestMantissaFromDouble = std::pow((double)10.0, (double)(PrintFloat::k_numberOfStoredSignificantDigits))-0.5;
|
||||
if (mantissaf >= biggestMantissaFromDouble) {
|
||||
exp++;
|
||||
}
|
||||
new (this) Decimal(Integer((int64_t)(std::round(mantissaf))), exp);
|
||||
}
|
||||
|
||||
Decimal::Decimal(Integer m, int e) :
|
||||
|
||||
@@ -252,10 +252,11 @@ QUIZ_CASE(poincare_decimal_to_text) {
|
||||
assert_expression_prints_to(Decimal(0.00000099999999999995), "9.9999999999995E-7", ScientificMode, 14);
|
||||
assert_expression_prints_to(Decimal(0.000000999999999999995), "0.000001", DecimalMode);
|
||||
assert_expression_prints_to(Decimal(0.000000999999999901200121020102010201201201021099995), "9.999999999012E-7", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(9999999999999.6), "9999999999999.6", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(99999999999999.6), "1E14", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(999999999999999.6), "1E15", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(9999999999999999.6), "1E16", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(9999999999999.54), "9999999999999.5", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(99999999999999.54), "1E14", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(999999999999999.54), "1E15", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(9999999999999999.54), "1E16", DecimalMode, 14);
|
||||
assert_expression_prints_to(Decimal(-9.702365051313E-297), "-9.702365051313E-297", DecimalMode, 14);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_approximation_to_text) {
|
||||
|
||||
Reference in New Issue
Block a user