[Poincare] Proper Integer approximation

This commit is contained in:
Romain Goyet
2015-09-16 19:14:11 +02:00
parent 003f6c4446
commit 6aedfb66e3
3 changed files with 9 additions and 4 deletions

View File

@@ -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:

View File

@@ -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;
}

View File

@@ -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);
}