Skip to content

Commit 5b134ac

Browse files
committed
ENH: Perform C division where appropriate
This PR performs C division where we know that we cannot encounter a division by zero.
1 parent 8583586 commit 5b134ac

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

numpy_financial/_cfinancial.pyx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ cdef double nper_inner_loop(
1313
return INFINITY
1414

1515
if rate_ == 0.0:
16-
return -(fv_ + pv_) / pmt_
16+
with cython.cdivision(True):
17+
# We know that pmt_ != 0, we don't need to check for division by 0
18+
return -(fv_ + pv_) / pmt_
1719

1820
if rate_ <= -1.0:
1921
return NAN
2022

21-
z = pmt_ * (1.0 + rate_ * when_) / rate_
23+
with cython.cdivision(True):
24+
# We know that rate_ != 0, we don't need to check for division by 0
25+
z = pmt_ * (1.0 + rate_ * when_) / rate_
2226
return log((-fv_ + z) / (pv_ + z)) / log(1.0 + rate_)
2327

2428

0 commit comments

Comments
 (0)