Skip to content

Commit

Permalink
Merge pull request #23 from uncountableinc/yagmin/divide-zero
Browse files Browse the repository at this point in the history
Custom error for divide by zero
  • Loading branch information
martinuncountable authored Oct 20, 2022
2 parents 740d631 + a837919 commit ea437a1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion hotxlfp/formulas/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
class XLError(RuntimeError):
pass

class ComputationValueError(XLError):
pass

ERROR = XLError('#ERROR!')
DIV_ZERO = XLError('#DIV/0!')
DIV_ZERO = ComputationValueError("#DIV/0!")
NAME = XLError('#NAME?')
NOT_AVAILABLE = XLError('#N/A')
NULL = XLError('#NULL!')
Expand Down
21 changes: 19 additions & 2 deletions hotxlfp/formulas/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@ def AND(*args):

@dispatcher.register_for('IF')
def IF(test, then, otherwise):
if isinstance(test, error.XLError):
return error.XLError
test_condition = torch.tensor(test, dtype=torch.bool)
if test_condition.all():
if isinstance(then, error.XLError):
return then
return torch.tensor(then, dtype=torch.double)
elif (~test_condition).all():
if isinstance(otherwise, error.XLError):
return otherwise
return torch.tensor(otherwise, dtype=torch.double)

if isinstance(then, error.XLError):
return then
if isinstance(otherwise, error.XLError):
return otherwise

return torch.where(
torch.tensor(test, dtype=torch.bool),
test_condition,
torch.tensor(then, dtype=torch.double),
torch.tensor(otherwise, dtype=torch.double)
torch.tensor(otherwise, dtype=torch.double),
)


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="hotxlfp",
version="0.0.11-unc19",
version="0.0.11-unc20",
packages=[
"hotxlfp",
"hotxlfp._compat",
Expand Down

0 comments on commit ea437a1

Please sign in to comment.