Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upadded type hints and doctests to arithmetic_analysis/newton_method.py #2259
Conversation
|
|
||
| # function is the f(x) and derivative is the f'(x) | ||
| def newton( | ||
| function: Callable[[float], float], |
This comment has been minimized.
This comment has been minimized.
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.
This comment has been minimized.
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.
This comment has been minimized.
| 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) |
This comment has been minimized.
This comment has been minimized.
cclauss
Aug 1, 2020
•
Member
| 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.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
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.
This comment has been minimized.
TravisBuddy
commented
Aug 1, 2020
|
Hey @spamegg1, TravisCI finished with status TravisBuddy Request Identifier: 7f7e1bf0-d3f9-11ea-9b7b-6b7bbdba586d |
|
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 |
|
It was my mistake... It is now fixed on master. Thanks! |


spamegg1 commentedAug 1, 2020
Continuing #2128
Also changed some variable names, made them more descriptive.
Describe your change:
Checklist: