Fix Integer approximation for 0.

Change-Id: Ib4bab6f4f776342bf6311d1e6f8b6d245d9a048a
This commit is contained in:
Felix Raimundo
2016-04-22 10:53:30 +02:00
parent 77f057de82
commit 6f5cc68b75
2 changed files with 12 additions and 0 deletions

View File

@@ -308,6 +308,15 @@ float Integer::approximate(Context& context) const {
mantissa |= (beforeLastDigit >> numberOfBitsInLastDigit);
}
if ((m_numberOfDigits==1) && (m_digits[0]==0)) {
/* This special case for 0 is needed, because the current algorithm assumes
* that the big integer is non zero, thus puts the exponent to 126 (integer
* area), the issue is that when the mantissa is 0, a "shadow bit" is
* assumed to be there, thus 126 0x000000 is equal to 0.5 and not zero.
*/
return m_negative ? -0.0f : 0.0f;
}
uint_result = 0;
uint_result |= (sign << 31);
uint_result |= (exponent << 23);

View File

@@ -54,4 +54,7 @@ QUIZ_CASE(poincare_integer_approximate) {
Context context;
assert(Integer(1).approximate(context) == 1.0f);
assert(Integer("12345678").approximate(context) == 12345678.0f);
assert(Integer("0").approximate(context) == 0);
assert(Integer("-0").approximate(context) == -0);
assert(Integer(-1).approximate(context) == -1);
}