Skip to content

Commit 03d87e5

Browse files
author
Kasper Peeters
committed
Catch division by zero when simplifying powers.
1 parent 2011909 commit 03d87e5

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

core/Algorithm.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ void Algorithm::propagate_zeroes(post_order_iterator& it, const iterator& topnod
337337
// Removing happens in the next step.
338338
}
339339
else if(*walk->name=="\\pow") {
340-
if(tr.index(it)==0) { // the argument
340+
if(tr.index(it)==0) { // the argument is zero
341+
sibling_iterator arg=it;
342+
++arg;
343+
if(*arg->multiplier<0)
344+
throw RuntimeException("Division by zero encountered.");
345+
341346
walk->multiplier=rat_set.insert(0).first;
342347
it=walk;
343348
propagate_zeroes(it, topnode);

core/Cleanup.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,14 @@ namespace cadabra {
149149
return true;
150150
}
151151

152+
if(*arg->multiplier==0) { // 0**positive = 0, 0**negative=undefined
153+
if(*exp->multiplier<0)
154+
throw RuntimeException("Division by zero encountered.");
155+
zero(it->multiplier);
156+
return true;
157+
}
158+
152159
if(*arg->name=="1") {
153-
if(*arg->multiplier==0) { // 0**anything = 0
154-
zero(it->multiplier);
155-
return true;
156-
}
157160
if(*arg->multiplier==1) { // 1**anything = 1
158161
one(it->multiplier);
159162
tr.erase_children(it);

tests/numerical.cdb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,23 @@ def test06():
9696

9797
test06()
9898

99-
99+
def test07():
100+
try:
101+
ex:=1/0;
102+
assert(1==0)
103+
except RuntimeError as err:
104+
print("Test 07a passed")
105+
try:
106+
ex:=1/a;
107+
substitute(ex, $a->0$)
108+
assert(1==0)
109+
except RuntimeError as err:
110+
print("Test 07b passed")
111+
try:
112+
ex:=1/(a-b);
113+
substitute(ex, $a->b$)
114+
assert(1==0)
115+
except RuntimeError as err:
116+
print("Test 07c passed")
117+
118+
test07()

0 commit comments

Comments
 (0)