diff --git a/src/common/FastRational.h b/src/common/FastRational.h index a3f4bb1cd..92dc3da3c 100644 --- a/src/common/FastRational.h +++ b/src/common/FastRational.h @@ -13,6 +13,7 @@ Copyright (c) 2008, 2009 Centre national de la recherche scientifique (CNRS) #include "Vec.h" #include #include +#include typedef int32_t word; typedef uint32_t uword; @@ -165,6 +166,7 @@ class FastRational } void force_ensure_mpq_valid() const { + // TK: I am afraid that this is UB const_cast(this)->ensure_mpq_valid(); } void ensure_mpq_memory_allocated() @@ -245,6 +247,20 @@ class FastRational } } + std::optional> 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(num), static_cast(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; }