X Tutup
The Wayback Machine - https://web.archive.org/web/20200914223442/https://github.com/TheAlgorithms/Python/pull/2259
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added type hints and doctests to arithmetic_analysis/newton_method.py #2259

Merged
merged 4 commits into from Aug 1, 2020

Conversation

@spamegg1
Copy link
Contributor

spamegg1 commented Aug 1, 2020

Continuing #2128
Also changed some variable names, made them more descriptive.

Describe your change:

  • Fix a bug or typo in an existing algorithm?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
Continuing #2128
Also changed some variable names, made them more descriptive.

# function is the f(x) and derivative is the f'(x)
def newton(
function: Callable[[float], float],

This comment has been minimized.

@cclauss

cclauss Aug 1, 2020

Member

Is there a way to defined a type for Callable[[float], float] and then use that type to simplify the call parameters?

This comment has been minimized.

@spamegg1

spamegg1 Aug 1, 2020

Author Contributor

Hmm... I think there is. I could try defining a type hint alias as described here:
https://docs.python.org/3/library/typing.html#type-aliases
Let me try and see if it passes mypy.

This comment has been minimized.

@spamegg1

spamegg1 Aug 1, 2020

Author Contributor

What do you know? It does work!

if derivative(prev_guess) == 0:
raise ZeroDivisionError("float division by zero, could not find root")

next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess)
Comment on lines 34 to 37

This comment has been minimized.

@cclauss

cclauss Aug 1, 2020

Member
Suggested change
if derivative(prev_guess) == 0:
raise ZeroDivisionError("float division by zero, could not find root")
next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess)
try:
next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess)
except ZeroDivisionError:
raise ZeroDivisionError("Could not find root")

Avoid calculating the derivative twice. The error type already says that we have a division by zero -- we do not need to repeat.

This comment has been minimized.

@spamegg1

spamegg1 Aug 1, 2020

Author Contributor

Makes sense, got it.

added a type alias for Callable[[float], float] and cleaned up the exception handling
if abs(prev_guess - next_guess) < 10 ** -5:
return next_guess
prev_guess = next_guess
Comment on lines 36 to 38

This comment has been minimized.

@cclauss

cclauss Aug 1, 2020

Member

Lines 36-38 can not raise a ZeroDivisionError so they should not be in the try block as discussed in PEP8.

This comment has been minimized.

@spamegg1

spamegg1 Aug 1, 2020

Author Contributor

OK, didn't know that. Understood.

spamegg1 and others added 2 commits Aug 1, 2020
improved exception handling
@cclauss
cclauss approved these changes Aug 1, 2020
@cclauss cclauss merged commit 473072b into TheAlgorithms:master Aug 1, 2020
1 of 2 checks passed
1 of 2 checks passed
codespell
Details
Travis CI - Pull Request Build Errored
Details
@TravisBuddy
Copy link

TravisBuddy commented Aug 1, 2020

Hey @spamegg1,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: 7f7e1bf0-d3f9-11ea-9b7b-6b7bbdba586d
@spamegg1
Copy link
Contributor Author

spamegg1 commented Aug 1, 2020

Hi @cclauss I know this got merged, but something weird must have happened during the merging process. In your last commit d55491a, Line 30 changed from prev_guess: float = starting_int (which is what I had in my last commit 8e20e61) to prev_guess float(starting_int) which makes the file currently broken. I already deleted my forked repo and realized this only later. If you could change it back it would be greatly appreciated. I probably should not make another PR over this.

@cclauss
Copy link
Member

cclauss commented Aug 1, 2020

It was my mistake... It is now fixed on master. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.
X Tutup