-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 50. Pow(x, n) #45
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
base: main
Are you sure you want to change the base?
Conversation
| > >>> round(0.1, 1) + round(0.1, 1) + round(0.1, 1) == round(0.3, 1) | ||
| > False | ||
| > >>> math.isclose(0.1 + 0.1 + 0.1, 0.3) | ||
| > False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここ転記ミスってます。Trueが正しいです。
| - bin32なら符号1bit、指数部8bits、仮数部23bits。単精度とも言われるもの。 | ||
| - bin64なら符号1bit、指数部11bits、仮数部52bits。倍精度とも言われるもの。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この桁数覚えておいてもいいでしょう。
nan, inf についても見ておいてください。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントありがとうございます。
どちらも指数部が全て1になるんですね。
| ## step3 | ||
| ```py | ||
| class Solution: | ||
| def _get_pow(self, base: float, exp: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、関数内関数のほうが良さそうに見えます。
n < 0 を入れられると間違った値をだしてしまうので、myPow経由で呼んでほしくなりました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ご指摘のとおりで、_get_powはnが0以上の整数であることが前提になっていました。で、関数内関数ならこの前提を強制できますね。
また、関数内関数にすることで失われる_get_powの再利用性も、そもそもmyPow自体を呼び出せばいい話ですね。
ネストが減っていいだろうくらいでやりましたが、色々考えることがあることに気付けました。ありがとうございます。
|
|
||
| ### 感想 | ||
| 浮動小数点周りはSWEの入り口として知っておくべきことが多そう。 | ||
| あと、一旦cを読めるように学習しないと、cpythonの読解の質が上がらなそう。 No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同感です。私もAI頼りに読んでいて、詳細まで読めていないです。
| raise ZeroDivisionError("When n < 0, x must be non-zero") | ||
| if x == 0: | ||
| if n == 0: | ||
| return 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing で float を返すと書いてあるにもかかわらず、 int を返している点に、違和感を感じました。好みの問題かもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントありがとうございます。
自分も違和感を感じ、step2からは1.0を返すようにしました。
| ```py | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| def get_pow(x: float, n: int) -> float: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get という英単語は、既に存在しているものを取得するというニュアンスがあるように思います。 calculate_pow() はいかがでしょうか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
get という英単語は、既に存在しているものを取得するというニュアンスがあるように思います。
これ意識できておらず、何でもかんでもget使っていました。calculateいいですね。
| half_pow_squared = half_pow * half_pow | ||
| if exp % 2 == 1: | ||
| return base * half_pow_squared | ||
| return half_pow_squared |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exp が偶数であれば末尾再帰さいてきかがかかるように書けそうです。
50. Pow(x, n)