Add signed long long constructors for Fraction

This commit is contained in:
Stepland 2022-12-27 19:35:58 +01:00
parent b0da3382ec
commit 33a14a6bda
2 changed files with 21 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <gmpxx.h>
#include <stdexcept>
@ -15,6 +16,24 @@ Fraction::Fraction(const unsigned long long a, const unsigned long long b) {
value = Fraction{a}.value / Fraction{b}.value;
}
Fraction::Fraction(const long long a) {
const unsigned long long abs = [&](){
if (a == INT64_MIN) {
return static_cast<unsigned long long>(INT64_MAX) + 1;
} else {
return static_cast<unsigned long long>(a < 0 ? -a : a);
}
}();
value = Fraction{abs}.value;
if (a < 0) {
value = -value;
}
}
Fraction::Fraction(const long long a, const long long b) {
value = Fraction{a}.value / Fraction{b}.value;
}
Fraction::Fraction(const Decimal& d) {
const auto reduced = d.reduce();
const mpq_class sign = (reduced.sign() > 0 ? 1 : -1);

View File

@ -23,6 +23,8 @@ public:
explicit Fraction(const unsigned long long a);
Fraction(const unsigned long long a, const unsigned long long b);
explicit Fraction(const long long a);
Fraction(const long long a, const long long b);
explicit Fraction(const Decimal& d);
explicit operator std::int64_t() const;
explicit operator std::uint64_t() const;