From 12693fc20f5c748fe2fb1fd21e5457f0c6d40320 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 30 Aug 2025 18:38:08 -0500 Subject: [PATCH] more operators --- include/bmath.hh | 31 +++++++++++++++++++++++++------ tests/test_add.cc | 4 +++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/bmath.hh b/include/bmath.hh index 8422c85..6e7881d 100644 --- a/include/bmath.hh +++ b/include/bmath.hh @@ -19,13 +19,13 @@ class mint { [[nodiscard]] constexpr explicit mint(T t) : value{t % M} {} [[nodiscard]] constexpr explicit operator T() const noexcept { return value; } - [[nodiscard]] constexpr mint operator+( - mint const other) const noexcept { + [[nodiscard]] constexpr mint operator+(mint const other) const noexcept { T result{add(static_cast(other.get()))}; return mint{result}; } - [[nodiscard]] constexpr mint& operator+=(mint const other) const noexcept { + + constexpr mint& operator+=(mint const other) const noexcept { value += other.value; return *this; @@ -44,7 +44,7 @@ class mint { return mint{result}; } - [[nodiscard]] constexpr mint& operator*=(mint const other) noexcept { + constexpr mint& operator*=(mint const other) noexcept { value *= other.value; return *this; @@ -61,7 +61,8 @@ class mint { return mint{result}; } - [[nodiscard]] constexpr mint& operator/=(mint const other) noexcept { + + constexpr mint& operator/=(mint const other) noexcept { if constexpr (other.get() == 0) { static_assert(false, "Cannot divide by 0"); } else if (other.get() == 0) { @@ -79,12 +80,30 @@ class mint { return mint{result}; } - [[nodiscard]] constexpr mint& operator%=(mint const other) const noexcept { + constexpr mint& operator%=(mint const other) const noexcept { value %= other.value; return *this; } + [[nodiscard]] constexpr mint operator-() const noexcept { + return mint{M - get()}; + } + + constexpr mint operator++() noexcept { + value += 1; + + return *this; + } + + constexpr mint operator++(int) noexcept { + auto _this = *this; + + value += 1; + + return _this; + } + [[nodiscard]] constexpr T get() const noexcept { return value; } [[nodiscard]] constexpr bool operator==(mint const other) const noexcept { diff --git a/tests/test_add.cc b/tests/test_add.cc index 3377ea5..a27b25d 100644 --- a/tests/test_add.cc +++ b/tests/test_add.cc @@ -30,7 +30,9 @@ int main() { // cout << (mint{5} + mint{7}); // cout << pow(mint{4}, 5); - cout << (pow(mint{5}, 5)); + // cout << to_string(pow(mint{5}, 5)); + + mint x{5}; return 0; }