Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7cff04

Browse files
wuminbinpoyea
authored andcommitted
better implementation for midpoint (#914)
1 parenta212efe commitb7cff04

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
whileabs(start-mid)>10**-7:# until we achieve precise equals to 10^-7
1919
iffunction(mid)==0:
2020
returnmid
2121
eliffunction(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
returnmid
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
whileleft<=right:
48-
midpoint=(left+right)//2
48+
midpoint=left+(right-left)//2
4949
current_item=sorted_collection[midpoint]
5050
ifcurrent_item==item:
5151
returnmidpoint

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp