Skip to content

Commit 6610b54

Browse files
committed
Add directional rounding based on sign
1 parent 9775e79 commit 6610b54

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

include/boost/decimal/detail/add_impl.hpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,40 @@ constexpr auto add_impl(const T& lhs, const T& rhs) noexcept -> ReturnType
117117
{
118118
// rounding mode == fe_dec_upward
119119
// Unconditionally round up. Could be 5e+95 + 4e-100 -> 5.000001e+95
120-
return big_lhs != 0U && (lhs_exp > rhs_exp) ?
121-
ReturnType{lhs.full_significand() + 1U, lhs.biased_exponent(), lhs.isneg()} :
122-
ReturnType{rhs.full_significand() + 1U, rhs.biased_exponent(), rhs.isneg()};
120+
const bool use_lhs {big_lhs != 0U && (lhs_exp > rhs_exp)};
121+
122+
if (use_lhs)
123+
{
124+
if (big_rhs != 0U)
125+
{
126+
if (lhs.isneg())
127+
{
128+
big_lhs -= 1U;
129+
}
130+
else
131+
{
132+
big_lhs += 1U;
133+
}
134+
}
135+
136+
return ReturnType{big_lhs, lhs.biased_exponent(), lhs.isneg()} ;
137+
}
138+
else
139+
{
140+
if (big_lhs != 0U)
141+
{
142+
if (rhs.isneg())
143+
{
144+
big_rhs -= 1U;
145+
}
146+
else
147+
{
148+
big_rhs += 1U;
149+
}
150+
}
151+
152+
return ReturnType{big_rhs, rhs.biased_exponent(), rhs.isneg()};
153+
}
123154
}
124155
}
125156

0 commit comments

Comments
 (0)