mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[Poincare] Proper Integer approximation
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user