Skip to content

Commit 4642f22

Browse files
committed
Add operator%
1 parent 398f5fa commit 4642f22

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

include/boost/decimal/decimal32_fast.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class decimal32_fast final
6161

6262
friend constexpr auto div_impl(decimal32_fast lhs, decimal32_fast rhs, decimal32_fast& q, decimal32_fast& r) noexcept -> void;
6363

64+
friend constexpr auto mod_impl(decimal32_fast lhs, decimal32_fast rhs, const decimal32_fast& q, decimal32_fast& r) noexcept -> void;
65+
6466
// Attempts conversion to integral type:
6567
// If this is nan sets errno to EINVAL and returns 0
6668
// If this is not representable sets errno to ERANGE and returns 0
@@ -115,12 +117,14 @@ class decimal32_fast final
115117
friend constexpr auto operator-(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast;
116118
friend constexpr auto operator*(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast;
117119
friend constexpr auto operator/(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast;
120+
friend constexpr auto operator%(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast;
118121

119122
// Compound operators
120123
constexpr auto operator+=(decimal32_fast rhs) noexcept -> decimal32_fast&;
121124
constexpr auto operator-=(decimal32_fast rhs) noexcept -> decimal32_fast&;
122125
constexpr auto operator*=(decimal32_fast rhs) noexcept -> decimal32_fast&;
123126
constexpr auto operator/=(decimal32_fast rhs) noexcept -> decimal32_fast&;
127+
constexpr auto operator%=(decimal32_fast rhs) noexcept -> decimal32_fast&;
124128

125129
// Increment and decrement
126130
constexpr auto operator++() noexcept -> decimal32_fast&;
@@ -523,6 +527,15 @@ constexpr auto div_impl(decimal32_fast lhs, decimal32_fast rhs, decimal32_fast&
523527
q = decimal32_fast(q_components.sig, q_components.exp, q_components.sign);
524528
}
525529

530+
constexpr auto mod_impl(decimal32_fast lhs, decimal32_fast rhs, const decimal32_fast& q, decimal32_fast& r) noexcept -> void
531+
{
532+
constexpr decimal32_fast zero {0, 0};
533+
534+
// https://en.cppreference.com/w/cpp/numeric/math/fmod
535+
auto q_trunc {q > zero ? floor(q) : ceil(q)};
536+
r = lhs - (decimal32_fast(q_trunc) * rhs);
537+
}
538+
526539
constexpr auto operator/(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast
527540
{
528541
decimal32_fast q {};
@@ -532,6 +545,22 @@ constexpr auto operator/(decimal32_fast lhs, decimal32_fast rhs) noexcept -> dec
532545
return q;
533546
}
534547

548+
constexpr auto operator%(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast
549+
{
550+
decimal32_fast q {};
551+
decimal32_fast r {};
552+
div_impl(lhs, rhs, q, r);
553+
mod_impl(lhs, rhs, q, r);
554+
555+
return r;
556+
}
557+
558+
constexpr auto decimal32_fast::operator%=(decimal32_fast rhs) noexcept -> decimal32_fast&
559+
{
560+
*this = *this % rhs;
561+
return *this;
562+
}
563+
535564
constexpr auto decimal32_fast::operator+=(decimal32_fast rhs) noexcept -> decimal32_fast&
536565
{
537566
*this = *this + rhs;

0 commit comments

Comments
 (0)