Skip to content

Commit c43a171

Browse files
committed
Apply similar fix to 128-bit addition path
1 parent a67a8ba commit c43a171

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

include/boost/decimal/detail/add_impl.hpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,35 @@ constexpr auto d128_add_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
173173
{
174174
// If we are subtracting even disparate numbers we need to round down
175175
// E.g. "5e+95"_DF - "4e-100"_DF == "4.999999e+95"_DF
176+
const auto use_lhs {lhs_sig != 0U && (lhs_exp > rhs_exp)};
176177

177-
return lhs_sig != 0U && (lhs_exp > rhs_exp) ?
178-
ReturnType{lhs_sig - static_cast<T>(lhs_sign != rhs_sign), lhs_exp, lhs_sign} :
179-
ReturnType{rhs_sig - static_cast<T>(lhs_sign != rhs_sign), rhs_exp, rhs_sign};
178+
// Need to check for the case where we have 1e+95 - anything = 9.99999... without losing a nine
179+
if (use_lhs)
180+
{
181+
const auto removed_zeros {detail::remove_trailing_zeros(lhs_sig)};
182+
if (removed_zeros.trimmed_number == 1U)
183+
{
184+
--lhs_sig;
185+
lhs_sig *= 10U;
186+
lhs_sig += 9U;
187+
--lhs_exp;
188+
}
189+
190+
return ReturnType{lhs_sig, lhs_exp, lhs_sign};
191+
}
192+
else
193+
{
194+
const auto removed_zeros {detail::remove_trailing_zeros(rhs_sig)};
195+
if (removed_zeros.trimmed_number == 1U)
196+
{
197+
--rhs_sig;
198+
rhs_sig *= 10U;
199+
rhs_sig += 9U;
200+
--rhs_exp;
201+
}
202+
203+
return ReturnType{rhs_sig, rhs_exp, rhs_sign};
204+
}
180205
}
181206
else
182207
{

0 commit comments

Comments
 (0)