Skip to content

Commit

Permalink
Implemented FastRational::(tryGetNumDen|getMpq)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomáš Kolárik authored and Martin Blicha committed Jan 29, 2024
1 parent 15111b2 commit a5b4adf
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/common/FastRational.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Copyright (c) 2008, 2009 Centre national de la recherche scientifique (CNRS)
#include "Vec.h"
#include <stack>
#include <vector>
#include <optional>

typedef int32_t word;
typedef uint32_t uword;
Expand Down Expand Up @@ -165,6 +166,7 @@ class FastRational
}
void force_ensure_mpq_valid() const
{
// TK: I am afraid that this is UB
const_cast<FastRational *>(this)->ensure_mpq_valid();
}
void ensure_mpq_memory_allocated()
Expand Down Expand Up @@ -245,6 +247,20 @@ class FastRational
}
}

std::optional<std::pair<word, uword>> tryGetNumDen() const {
if (!wordPartValid()) return {};
return std::make_pair(num, den);
}

mpq_class getMpq() const {
if (wordPartValid()) {
static_assert(sizeof(long) == 8);
return mpq_class{static_cast<long>(num), static_cast<long>(den)};
}
assert(mpqPartValid());
return mpq_class{mpq};
}

inline int compare(const FastRational& b) const;
inline int sign() const;
bool operator< ( const FastRational & b ) const { return compare(b) < 0; }
Expand Down

0 comments on commit a5b4adf

Please sign in to comment.