Skip to content

Create 50. Pow(x, n).md#45

Open
fuga-98 wants to merge 1 commit into
mainfrom
50.-Pow(x,-n)
Open

Create 50. Pow(x, n).md#45
fuga-98 wants to merge 1 commit into
mainfrom
50.-Pow(x,-n)

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 14, 2025

Comment thread 50. Pow(x, n).md
https://github.com/Yoshiki-Iwasa/Arai60/pull/38/files#r1711522299

- 小数点にびくびくするという話。
- 確かに。でもPythonならなんとかしているでしょ、みたいな気持ちがありました。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python の小数点は通常 IEEE 754 で多くの言語と同じです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ドキュメントを読みました。
https://docs.python.org/ja/3.13/tutorial/floatingpoint.html
誤差がどの程度許容されるかは気にするべきでした。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

https://ja.m.wikipedia.org/wiki/IEEE_754
浮動小数点の規格があることに驚きました。

Comment thread 50. Pow(x, n).md

https://github.com/TORUS0818/leetcode/pull/47/files#r2038337006

n, xを破壊しない。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

場合にもよりますが、何をしているのかが分かりにくくなることが多いかと思います。

Comment thread 50. Pow(x, n).md
return result
```

x=0.00001, n=2147483647で時間オーバー
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

時間計算量は求めましたか?時間計算量にデータサイズを代入して、おおよその計算ステップ数を求めましたか?計算ステップ数からおおよその処理時間を求めましたか?

x=0.00001, n=2147483647で時間オーバー

こちらのケースでどれくらいの時間がかかるか求めてみることをお勧めいたします。

おおよその計算時間の求め方は
https://discord.com/channels/1084280443945353267/1295324533963751465/1322110315667652710
をご覧ください。

関数を書き始める前に、時間計算量・空間計算量を求め、おおよその処理時間・使用メモリ量を求め、利用可能なコンピュータリソース内に収まっていることを確認する癖をつけることをお勧めいたします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

レビューありがとうございます。
どうせこれじゃないだろうと思いながらやっていたので、求めるのを忘れていました。
意識しようと思います。

Comment thread 50. Pow(x, n).md
if x > 0:
log_x = log2(x)
result_log = log_x * n
result = pow(2, result_log)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この問題の趣旨は pow() 関数を自作することだと思います。 myPow() の中で pow() を使うと、趣旨から外れるように思います。

Comment thread 50. Pow(x, n).md
bit <<= 1
return result
```
bit演算なんか楽しいですね。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

面白い方法ですね。

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.

4 participants