Skip to content

Commit b7cff04

Browse files
wuminbinpoyea
authored andcommitted
better implementation for midpoint (#914)
1 parent a212efe commit b7cff04

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

arithmetic_analysis/bisection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
1414
print("couldn't find root in [a,b]")
1515
return
1616
else:
17-
mid = (start + end) / 2
17+
mid = start + (end - start) / 2.0
1818
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
1919
if function(mid) == 0:
2020
return mid
2121
elif function(mid) * function(start) < 0:
2222
end = mid
2323
else:
2424
start = mid
25-
mid = (start + end) / 2
25+
mid = start + (end - start) / 2.0
2626
return mid
2727

2828

searches/binary_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def binary_search(sorted_collection, item):
4545
right = len(sorted_collection) - 1
4646

4747
while left <= right:
48-
midpoint = (left + right) // 2
48+
midpoint = left + (right - left) // 2
4949
current_item = sorted_collection[midpoint]
5050
if current_item == item:
5151
return midpoint

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy