-
Notifications
You must be signed in to change notification settings - Fork 0
50. Pow(x,n) #43
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?
50. Pow(x,n) #43
Conversation
remainder := n % 2 | ||
if remainder == 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.
このremainderはなくてもいいかもと思いました。
if n == 1 { | ||
return x * accumulatedProduct | ||
} |
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.
ここなくても下(L.391~L.392)で拾えるので、問題なく動きそうですかね?(何か意図があったらすみません...)
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.
無限ループなのでこれがないと終了しません。
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.
L.391~L.392 はnが奇数の場合の処理です。
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.
あ、Goってwhile文がないんですね...にわかすぎて失礼しました。
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.
while文ないですが、
for n > 1 {
と書けばwhile文と同じような挙動となり、return文をループの外に持ってくることが可能です。
- 2cで調べた標準ライブラリの内部実装を参考に特殊ケースをちゃんと処理したもの | ||
- nをfloat型に変換することによりnがMinIntの場合にも対応 |
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.
私はここまで特殊ケース網羅的に考慮できていなかったので、すごいなあと思いました。
20. Pow(x, y) = NaN for finite x < 0 and finite non-integer y | ||
- これはよくわからなかった |
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/2 乗って要するにルートのことですよね。
x^y は x が負で y が整数でない場合は、-2 の 1/2 乗などです。これ見たことありますか。高校範囲だと虚数ですね。
冪も複素数範囲に拡張することはできますが、一般に、値を複数持つ多価関数として扱うことが多いです。
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.
なるほどです。小数の冪乗がルートで表せることが頭から抜けてました。
5. Pow(x, NaN) = NaN | ||
- 1~5はすぐ納得 | ||
6. Pow(±0, y) = ±Inf for y an odd integer < 0 | ||
- 0の冪乗は0だと思っていたが、負の冪乗の場合は偶奇によって±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.
0^0 == 0 にすることが多いですが、どちらにしてもどちらから近づくかで極限が異なるので難しい問題をはらんでいます。
定義の問題にしてしまうことが多いです。
この場合、0^0は1.で扱われていますね。
- nが負の場合の処理方法として、nを正にして1/x^(-n)と解釈する方法もある | ||
- https://github.com/SanakoMeine/leetcode/pull/9/files#diff-579f0a929d810b241709f7ffe2baa254c89984745a8b4f97490b2bb4b3e04903R39 | ||
- https://github.com/SanakoMeine/leetcode/pull/9/files#diff-579f0a929d810b241709f7ffe2baa254c89984745a8b4f97490b2bb4b3e04903R83 | ||
- 自分も再帰の方がしっくりきた |
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.
この再帰をループに直すのは、自然に見えて(あまり違いがないように見えて)欲しいです。
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.
2^7 = 4^3 * 2 という数式変換をしているという風に理解していたので直感的に再帰だと思っていました。
ループの方はaccumulatedProduct
におこぼれを累積していくところがしっくりきていなかったです。
- mantissa: 0以外 | ||
- https://www.geeksforgeeks.org/ieee-standard-754-floating-point-numbers/ | ||
- 単精度: single-precision 32bit | ||
- 倍精度: double-precision 64bit |
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.
IEEE-754の内部ビットの数も覚えておくと、面接でよく分かっている風が醸せることがあります。
exponent が8ビットと11ビットです。符号が1ビットで残りが23ビットと52ビットです。
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+8+23 = 32 と 1+11+52 = 64 なので exponent さえ覚えていれば mantissa は計算できますね。
あとはバイアスの 2^7 - 1 = 127 と 2^10 - 1 = 1023 も exponent を覚えていればわかりますね。
https://leetcode.com/problems/powx-n/description/