From 7326815dee2d01454eac57ad5379a6f4a1aa67ef Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Wed, 9 Sep 2020 11:15:12 +0100 Subject: [PATCH 1/2] Add negative input handling --- ballpark/utils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ballpark/utils.py b/ballpark/utils.py index d012f6f..e4697b4 100644 --- a/ballpark/utils.py +++ b/ballpark/utils.py @@ -78,7 +78,6 @@ def quantize(number, digits=0, q=builtins.round): def repel(value): return math.copysign(max(abs(value), 1e-24), value) - def unwrap(fn): @functools.wraps(fn) def unwrapped_function(values, *vargs, **kwargs): @@ -87,7 +86,15 @@ def unwrapped_function(values, *vargs, **kwargs): if scalar: values = [values] - results = fn(values, *vargs, **kwargs) + # Handle negative inputs by converting to positive, then correcting the output + + is_negative = list(map(lambda x : x < 0, values)) + multipliers = [-1 if negative else 1 for negative in is_negative] + positive_inputs = [value * multiplier for value, multiplier in zip(values, multipliers)] + + positive_results = fn(positive_inputs, *vargs, **kwargs) + + results = ["-" + positive_result if negative else positive_result for positive_result, negative in zip(positive_results, is_negative)] if scalar: results = results[0] From 77349a8d74d90834a39cecefc204fc14d03f4d76 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Wed, 9 Sep 2020 11:16:51 +0100 Subject: [PATCH 2/2] Replace removed whitespace --- ballpark/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ballpark/utils.py b/ballpark/utils.py index e4697b4..18a0489 100644 --- a/ballpark/utils.py +++ b/ballpark/utils.py @@ -78,6 +78,7 @@ def quantize(number, digits=0, q=builtins.round): def repel(value): return math.copysign(max(abs(value), 1e-24), value) + def unwrap(fn): @functools.wraps(fn) def unwrapped_function(values, *vargs, **kwargs):