From 33a14a6bda6fef7d53ce459ab521c369aacdcd85 Mon Sep 17 00:00:00 2001 From: Stepland <10530295-Buggyroom@users.noreply.gitlab.com> Date: Tue, 27 Dec 2022 19:35:58 +0100 Subject: [PATCH] Add signed long long constructors for Fraction --- src/special_numeric_types.cpp | 19 +++++++++++++++++++ src/special_numeric_types.hpp | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/special_numeric_types.cpp b/src/special_numeric_types.cpp index f2e20d2..c4f4380 100644 --- a/src/special_numeric_types.cpp +++ b/src/special_numeric_types.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -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(INT64_MAX) + 1; + } else { + return static_cast(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); diff --git a/src/special_numeric_types.hpp b/src/special_numeric_types.hpp index 9bc5df0..9b55876 100644 --- a/src/special_numeric_types.hpp +++ b/src/special_numeric_types.hpp @@ -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;