From 6aedfb66e317dfce315cc7b48e88cd33219453c3 Mon Sep 17 00:00:00 2001 From: Romain Goyet Date: Wed, 16 Sep 2015 19:14:11 +0200 Subject: [PATCH] [Poincare] Proper Integer approximation --- poincare/include/poincare/integer.h | 1 + poincare/src/integer.cpp | 8 ++++---- poincare/test/integer.cpp | 4 ++++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/poincare/include/poincare/integer.h b/poincare/include/poincare/integer.h index 74545560b..e8bc00045 100644 --- a/poincare/include/poincare/integer.h +++ b/poincare/include/poincare/integer.h @@ -19,6 +19,7 @@ class Integer : public Expression { const Integer operator+(const Integer &other) const; const Integer operator*(const Integer &other) const; bool operator==(const Integer &other) const; + virtual float approximate(); protected: virtual void layout(); private: diff --git a/poincare/src/integer.cpp b/poincare/src/integer.cpp index decc019b5..4a1ba45be 100644 --- a/poincare/src/integer.cpp +++ b/poincare/src/integer.cpp @@ -162,15 +162,15 @@ float Integer::approximate() { * * We can tell that: * - the sign is going to be 0 for now, we only handle positive numbers - * - the exponent is the length of our BigInt, in bits + 127 - * - the mantissa is the beginning of our BigInt + * - the exponent is the length of our BigInt, in bits - 1 + 127; + * - the mantissa is the beginning of our BigInt, discarding the first bit */ //bool sign = 0; native_uint_t lastDigit = m_digits[m_numberOfDigits-1]; uint8_t numberOfBitsInLastDigit = log2(lastDigit); - uint8_t exponent = 127; + uint8_t exponent = 126; exponent += (m_numberOfDigits-1)*32; exponent += numberOfBitsInLastDigit; @@ -184,7 +184,7 @@ float Integer::approximate() { uint_result = 0; //uint_result |= (sign << 31); uint_result |= (exponent << 23); - uint_result |= (mantissa >> (32-23) & 0x7FFFFF); + uint_result |= (mantissa >> (32-23-1) & 0x7FFFFF); return float_result; } diff --git a/poincare/test/integer.cpp b/poincare/test/integer.cpp index 100013fff..28c0a2fbd 100644 --- a/poincare/test/integer.cpp +++ b/poincare/test/integer.cpp @@ -21,3 +21,7 @@ QUIZ_CASE(poincare_integer_multiply) { QUIZ_CASE(poincare_integer_parse_integer) { assert(Integer::parseInteger("123") == Integer(123)); } + +QUIZ_CASE(poincare_integer_approximate) { + assert(Integer::parseInteger("12345678").approximate() == 12345678.0f); +}