From b304f83c551eded262d3e0deaeb44784150ae1dc Mon Sep 17 00:00:00 2001 From: Catherine Lasersohn Date: Wed, 25 Oct 2023 14:41:20 -0400 Subject: [PATCH 1/3] handle ceiling function --- hotxlfp/formulas/mathtrig.py | 37 ++++++++++++++++++++++++------------ setup.py | 2 +- tests/test_formula_parser.py | 4 ++++ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/hotxlfp/formulas/mathtrig.py b/hotxlfp/formulas/mathtrig.py index 6087f62..85686cf 100644 --- a/hotxlfp/formulas/mathtrig.py +++ b/hotxlfp/formulas/mathtrig.py @@ -245,23 +245,36 @@ def SUMIF(args, criteria): @dispatcher.register_for("CEILING", "CEILING.MATH", "CEILING.PRECISE") def CEILING(number, significance=1): - number = utils.parse_number(number) + number = torch.tensor(utils.parse_number(number)) significance = utils.parse_number(significance) + if not isinstance(significance, torch.Tensor) or significance.size(dim=0) == 1: + significance = torch.broadcast_to(torch.tensor(significance), number.size()) if utils.any_is_error((number, significance)): return error.VALUE - if significance == 0: - return 0 + if number.size(dim=0) != significance.size(dim=0): + return error.VALUE - positive_significance = significance > 0 - significance = abs(significance) - if number >= 0: - return math.ceil(number / significance) * significance - else: - if positive_significance: - return -1 * math.floor(abs(number) / significance) * significance - else: - return -1 * math.ceil(abs(number) / significance) * significance + positive_number = torch.where( + (number >= 0) & (significance != 0), + torch.ceil(number / significance) * significance, + 0, + ) + + positive_significance = torch.where( + (number < 0) & (significance > 0), + -1 * torch.floor(torch.abs(number) / significance) * significance, + 0, + ) + + negative_significance = torch.where( + (number < 0) & (significance < 0), + -1 * torch.ceil(torch.abs(number) / significance) * significance, + 0, + ) + + results = positive_number + positive_significance + negative_significance + return results @dispatcher.register_for("FLOOR", "FLOOR.MATH", "FLOOR.PRECISE") diff --git a/setup.py b/setup.py index f47e75e..2afdb27 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="hotxlfp", - version="0.0.11+unc.26", + version="0.0.11+unc.27", packages=[ "hotxlfp", "hotxlfp._compat", diff --git a/tests/test_formula_parser.py b/tests/test_formula_parser.py index aded048..8cee2d3 100644 --- a/tests/test_formula_parser.py +++ b/tests/test_formula_parser.py @@ -375,6 +375,10 @@ def test_tensors(self): _test_equation(equation="MAX(MAX(2, a1 * 2), 100)", variables={"a1": [5, 4]}, answer=[100, 100]) _test_equation(equation="5", variables={"a1": [5, 4]}, answer=[5]) _test_equation(equation="SQRT(100)", variables={"a1": [5]}, answer=[10]) + _test_equation(equation="CEILING(a1)", variables={"a1": [4.5, -1.2]}, answer=[5, -1]) + _test_equation(equation="CEILING(a1, a2)", variables={"a1": [0.5, 0.5], "a2": [1, 2]}, answer=[1, 2]) + _test_equation(equation="CEILING(a1, a2)", variables={"a1": [0.5, 0.5], "a2": [2]}, answer=[2, 2]) + _test_equation(equation="CEILING(a1, a2)", variables={"a1": [0.5], "a2": [1]}, answer=[1]) def test_scientific_notation(self): _test_equation(equation="2e2", variables={"a1" : [1.1]}, answer=[200]) From 2c7d0c1711c537974379093c1dd3e69fae2beed8 Mon Sep 17 00:00:00 2001 From: Catherine Lasersohn Date: Wed, 25 Oct 2023 15:03:56 -0400 Subject: [PATCH 2/3] add abs --- hotxlfp/formulas/mathtrig.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hotxlfp/formulas/mathtrig.py b/hotxlfp/formulas/mathtrig.py index 85686cf..71dcdf4 100644 --- a/hotxlfp/formulas/mathtrig.py +++ b/hotxlfp/formulas/mathtrig.py @@ -257,7 +257,7 @@ def CEILING(number, significance=1): positive_number = torch.where( (number >= 0) & (significance != 0), - torch.ceil(number / significance) * significance, + torch.ceil(number / abs(significance)) * abs(significance), 0, ) @@ -269,7 +269,7 @@ def CEILING(number, significance=1): negative_significance = torch.where( (number < 0) & (significance < 0), - -1 * torch.ceil(torch.abs(number) / significance) * significance, + -1 * torch.ceil(torch.abs(number) / abs(significance)) * abs(significance), 0, ) From 9e8688187e1848a04135860695c74bddd9f64529 Mon Sep 17 00:00:00 2001 From: Catherine Lasersohn Date: Wed, 25 Oct 2023 15:05:00 -0400 Subject: [PATCH 3/3] add abs --- hotxlfp/formulas/mathtrig.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hotxlfp/formulas/mathtrig.py b/hotxlfp/formulas/mathtrig.py index 71dcdf4..89be5a4 100644 --- a/hotxlfp/formulas/mathtrig.py +++ b/hotxlfp/formulas/mathtrig.py @@ -257,7 +257,7 @@ def CEILING(number, significance=1): positive_number = torch.where( (number >= 0) & (significance != 0), - torch.ceil(number / abs(significance)) * abs(significance), + torch.ceil(number / torch.abs(significance)) * torch.abs(significance), 0, ) @@ -269,7 +269,7 @@ def CEILING(number, significance=1): negative_significance = torch.where( (number < 0) & (significance < 0), - -1 * torch.ceil(torch.abs(number) / abs(significance)) * abs(significance), + -1 * torch.ceil(torch.abs(number) / torch.abs(significance)) * torch.abs(significance), 0, )