From 95ce07dfc76107bb19a0259130c511150414da0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Thu, 30 Nov 2017 17:39:04 +0100 Subject: [PATCH] [poincare] In Integer::approximate, exclude special case 0 first to avoid breaking assertions Change-Id: I7dea2e05a485a581251b43be1814f00615f17594 --- poincare/src/integer.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index 72622403e..1c17bf2c3 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -473,6 +473,15 @@ IntegerDivision Integer::udiv(const Integer & numerator, const Integer & denomin template T Integer::approximate() const { + if (isZero()) { + /* 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. + */ + T result = m_negative ? -0.0 : 0.0; + return result; + } union { uint64_t uint_result; T float_result; @@ -544,16 +553,6 @@ T Integer::approximate() const { // shift the mantissa if the rounding increase the length in bits of the // mantissa. - if (isZero()) { - /* 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. - */ - T result = m_negative ? -0.0 : 0.0; - return result; - } - u.uint_result = 0; u.uint_result |= ((uint64_t)sign << (totalNumberOfBits-1)); u.uint_result |= ((uint64_t)exponent << mantissaNbBit);