Skip to content

Commit f94991b

Browse files
committed
Detect pure powers of ten to avoid losing digit
1 parent ce5cea3 commit f94991b

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

include/boost/decimal/detail/add_impl.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,44 @@ constexpr auto add_impl(const T& lhs, const T& rhs) noexcept -> ReturnType
124124
{
125125
if (lhs.isneg())
126126
{
127-
big_lhs -= 1U;
127+
if (is_power_of_10(big_lhs))
128+
{
129+
--big_lhs;
130+
big_lhs *= 10U;
131+
big_lhs += 9U;
132+
--lhs_exp;
133+
}
134+
else
135+
{
136+
--big_lhs;
137+
}
128138
}
129139
else
130140
{
131-
big_lhs += 1U;
141+
++big_lhs;
132142
}
133143
}
134144

135-
return ReturnType{big_lhs, lhs.biased_exponent(), lhs.isneg()} ;
145+
return ReturnType{big_lhs, lhs_exp, lhs.isneg()} ;
136146
}
137147
else
138148
{
139149
if (big_lhs != 0U)
140150
{
141151
if (rhs.isneg())
142152
{
143-
big_rhs -= 1U;
153+
--big_rhs;
154+
big_rhs *= 10U;
155+
big_rhs += 9U;
156+
--rhs_exp;
144157
}
145158
else
146159
{
147-
big_rhs += 1U;
160+
++big_rhs;
148161
}
149162
}
150163

151-
return ReturnType{big_rhs, rhs.biased_exponent(), rhs.isneg()};
164+
return ReturnType{big_rhs, rhs_exp, rhs.isneg()};
152165
}
153166
}
154167
}
@@ -224,7 +237,7 @@ constexpr auto d128_add_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
224237
{
225238
if (rhs_sig != 0U && (lhs_sign != rhs_sign))
226239
{
227-
if (is_power_of_ten(lhs_sig))
240+
if (is_power_of_10(lhs_sig))
228241
{
229242
--lhs_sig;
230243
lhs_sig *= 10U;
@@ -243,7 +256,7 @@ constexpr auto d128_add_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
243256
{
244257
if (lhs_sig != 0U && (lhs_sign != rhs_sign))
245258
{
246-
if (is_power_of_ten(rhs_sig))
259+
if (is_power_of_10(rhs_sig))
247260
{
248261
--rhs_sig;
249262
rhs_sig *= 10U;

0 commit comments

Comments
 (0)