Skip to content

Conversation

hroc135
Copy link
Owner

@hroc135 hroc135 commented Mar 17, 2025

Comment on lines +126 to +127
remainder := n % 2
if remainder == 1 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このremainderはなくてもいいかもと思いました。

Comment on lines +388 to +390
if n == 1 {
return x * accumulatedProduct
}
Copy link

@olsen-blue olsen-blue Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここなくても下(L.391~L.392)で拾えるので、問題なく動きそうですかね?(何か意図があったらすみません...)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

無限ループなのでこれがないと終了しません。

Copy link
Owner Author

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が奇数の場合の処理です。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あ、Goってwhile文がないんですね...にわかすぎて失礼しました。

Copy link
Owner Author

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文をループの外に持ってくることが可能です。

Comment on lines +291 to +292
- 2cで調べた標準ライブラリの内部実装を参考に特殊ケースをちゃんと処理したもの
- nをfloat型に変換することによりnがMinIntの場合にも対応

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

私はここまで特殊ケース網羅的に考慮できていなかったので、すごいなあと思いました。

Comment on lines +202 to +203
20. Pow(x, y) = NaN for finite x < 0 and finite non-integer y
- これはよくわからなかった
Copy link

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 乗などです。これ見たことありますか。高校範囲だと虚数ですね。

冪も複素数範囲に拡張することはできますが、一般に、値を複数持つ多価関数として扱うことが多いです。

Copy link
Owner Author

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になる
Copy link

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
- 自分も再帰の方がしっくりきた
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この再帰をループに直すのは、自然に見えて(あまり違いがないように見えて)欲しいです。

Copy link
Owner Author

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
Copy link

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ビットです。

Copy link
Owner Author

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 を覚えていればわかりますね。

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

Successfully merging this pull request may close these issues.

3 participants