Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW] Clip decimal binary op precision at max precision #8194

Merged
merged 6 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions python/cudf/cudf/core/column/decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ def _binop_precision(l_dtype, r_dtype, op):
p1, p2 = l_dtype.precision, r_dtype.precision
s1, s2 = l_dtype.scale, r_dtype.scale
if op in ("add", "sub"):
return max(s1, s2) + max(p1 - s1, p2 - s2) + 1
result = max(s1, s2) + max(p1 - s1, p2 - s2) + 1
elif op in ("mul", "div"):
return p1 + p2 + 1
result = p1 + p2 + 1
else:
raise NotImplementedError()

return min(result, cudf.Decimal64Dtype.MAX_PRECISION)
22 changes: 11 additions & 11 deletions python/cudf/cudf/tests/test_binops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1772,11 +1772,11 @@ def _decimal_series(input, dtype):
(
operator.add,
["100", "200"],
cudf.Decimal64Dtype(scale=-2, precision=3),
cudf.Decimal64Dtype(scale=-2, precision=17),
["0.1", "0.2"],
cudf.Decimal64Dtype(scale=3, precision=4),
["100.1", "200.2"],
cudf.Decimal64Dtype(scale=3, precision=9),
cudf.Decimal64Dtype(scale=3, precision=18),
),
(
operator.sub,
Expand All @@ -1799,11 +1799,11 @@ def _decimal_series(input, dtype):
(
operator.sub,
["100", "200"],
cudf.Decimal64Dtype(scale=-2, precision=3),
cudf.Decimal64Dtype(scale=-2, precision=10),
["0.1", "0.2"],
cudf.Decimal64Dtype(scale=3, precision=4),
cudf.Decimal64Dtype(scale=6, precision=10),
["99.9", "199.8"],
cudf.Decimal64Dtype(scale=3, precision=9),
cudf.Decimal64Dtype(scale=6, precision=18),
),
(
operator.mul,
Expand Down Expand Up @@ -1853,11 +1853,11 @@ def _decimal_series(input, dtype):
(
operator.truediv,
["132.86", "15.25"],
cudf.Decimal64Dtype(scale=4, precision=6),
cudf.Decimal64Dtype(scale=4, precision=14),
["2.34", "8.50"],
cudf.Decimal64Dtype(scale=2, precision=4),
cudf.Decimal64Dtype(scale=2, precision=8),
["56.77", "1.79"],
cudf.Decimal64Dtype(scale=2, precision=11),
cudf.Decimal64Dtype(scale=2, precision=18),
),
(
operator.add,
Expand Down Expand Up @@ -1907,11 +1907,11 @@ def _decimal_series(input, dtype):
(
operator.mul,
["100", "200"],
cudf.Decimal64Dtype(scale=-2, precision=3),
cudf.Decimal64Dtype(scale=-2, precision=10),
["0.1", None],
cudf.Decimal64Dtype(scale=3, precision=4),
cudf.Decimal64Dtype(scale=3, precision=12),
["10.0", None],
cudf.Decimal64Dtype(scale=1, precision=8),
cudf.Decimal64Dtype(scale=1, precision=18),
),
(
operator.eq,
Expand Down