[TMP] Refactor mantissaExponent()

This commit is contained in:
Kamil Śliwak 2022-07-12 15:41:27 +02:00
parent 216df46ff4
commit 15fa973569
2 changed files with 21 additions and 14 deletions

View File

@ -1224,27 +1224,29 @@ FixedPointType const* RationalNumberType::fixedPointType() const
pair<RationalNumberType const*, RationalNumberType const*> RationalNumberType::mantissaExponent() const pair<RationalNumberType const*, RationalNumberType const*> RationalNumberType::mantissaExponent() const
{ {
bool negative = (m_value < 0); rational const maxUint = rational((bigint(1) << 256) - 1);
int exponent = 0; rational const minInt = -rational(bigint(1) << 255);
rational value = abs(m_value); // We care about the sign later.
rational maxValue = negative ?
rational(bigint(1) << 255, 1):
rational((bigint(1) << 256) - 1, 1);
while (value.denominator() != 1) bool negative = (m_value < 0);
rational const maxMantissa = (negative ? -minInt : maxUint);
int exponent = 0;
rational unsignedMantissa = abs(m_value);
while (unsignedMantissa.denominator() != 1)
{ {
value *= 10; unsignedMantissa *= 10;
exponent--; --exponent;
if ( if (
value > rational((bigint(1) << 256) - 1) || unsignedMantissa > maxUint ||
value < rational(-(bigint(1) << 255)) || unsignedMantissa < minInt ||
exponent < -255 // TODO sane vale? exponent < -255 // TODO: sane value?
) )
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
return { return {
TypeProvider::rationalNumber(value), TypeProvider::rationalNumber(unsignedMantissa),
TypeProvider::rationalNumber(-exponent), TypeProvider::rationalNumber(-exponent),
}; };
} }

View File

@ -587,7 +587,12 @@ public:
/// @returns true if the value is not an integer. /// @returns true if the value is not an integer.
bool isFractional() const { return m_value.denominator() != 1; } bool isFractional() const { return m_value.denominator() != 1; }
/// TODO document // TODO: Update if it turns out we do need to support negative numbers here.
/// Tries to decompose a positive rational number into two positive integers - a mantissa and a
/// base-10 exponent, such that the number is equal to `mantissa * 10**-exponent`.
/// @returns Pair of non-null pointers representing the types of the literals corresponding to
/// mantissa and exponent if the resulting mantissa and exponent both fit in 256 bits.
/// A pair of null pointers otherwise.
std::pair<RationalNumberType const*, RationalNumberType const*> mantissaExponent() const; std::pair<RationalNumberType const*, RationalNumberType const*> mantissaExponent() const;
/// @returns true if the value is negative. /// @returns true if the value is negative.