Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 13, 2025

return 1;
}
if (n < 0) {
return 1 / myPow(x, -n);
Copy link

Choose a reason for hiding this comment

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

自分は浮動小数は 1.0 のように浮動小数であることを明示して書くことが多いです。趣味の範囲かもしれません。

参考までにスタイルガイドへのリンクを貼ります。

https://google.github.io/styleguide/cppguide.html#Floating_Literals

Floating-point literals should always have a radix point, with digits on both sides, even if they use exponential notation. Readability is improved if all floating-point literals take this familiar form, as this helps ensure that they are not mistaken for integer literals, and that the E/e of the exponential notation is not mistaken for a hexadecimal digit. It is fine to initialize a floating-point variable with an integer literal (assuming the variable type can exactly represent that integer), but note that a number in exponential notation is never an integer literal.

上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

if (n < 0) {
return 1 / myPow(x, -n);
}
vector<long long> power_of_2_cache = {1};
Copy link

Choose a reason for hiding this comment

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

2 の累乗は 1 << x のシフト演算で高速に計算することができます。 vector でキャッシュすると、ヒープメモリの確保やメモリアクセスなどで、シフト演算より遅くなると思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかにシフト演算でできますね!
c++にはべき乗の演算子がなかったのでどうしようと考え、for文で回す方法しか思い付きませんでした。

// cast to long long to handle INT_MIN;
return 1.0 / _myPow(x, -(long long)n);
}
return _myPow(x, n);
Copy link

Choose a reason for hiding this comment

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

関数名の先頭にアンダースコアを付けるのは、あまり見ないように思います。チームの平均的な書き方に合わせることをおすすめします。

参考までに、識別子に 2 つの連続するアンダースコアを使用したり、 1 つのアンダースコアのあとに 1 つの大文字が続くものは、 C++ の実装のために予約されています。これらの識別子は使用しないようにしてください。

https://learn.microsoft.com/ja-jp/cpp/cpp/identifiers-cpp?view=msvc-170

識別子に 2 つの連続するアンダースコア文字 (__) を使用した場合、または 1 つのアンダースコア文字の後に 1 つの大文字が続く場合は、すべてのスコープで C++ の実装のために予約されています。 現在または将来の予約済み識別子と競合する可能性があるため、ファイルのスコープを含む名前には、先頭の 1 つのアンダースコアとそれに続く子文字を使用しないようにする必要があります。

Copy link

Choose a reason for hiding this comment

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

C++ のアンダースコアーから始まる識別子については、次の文字が大文字またはアンダースコアーならば予約済みで、小文字の場合は、グローバルスコープでは予約済みです。
メンバ変数やローカル変数で使うことは言語仕様上は構いませんがグローバルスコープで将来予約される可能性があるため積極的にはしない気がしますね。
https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

Copy link
Owner Author

@potrue potrue Aug 13, 2025

Choose a reason for hiding this comment

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

詳しくありがとうございます。基本的には避けるようにしようと思います。

double _myPow(double x, long long n) {
double result = 1.0;
while (n) {
if (n % 2 == 1) {
Copy link

Choose a reason for hiding this comment

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

(n & 1) と書く方もいると思います。趣味の範囲だと思います。

ただし、 CPU の割り算の命令は一般的に遅いです。そのため、コンパイラーは等価なビット演算に変換できる場合は、それらに置き換える場合があります。この辺りは意識しておいたほうが良いと思います。

以下も参考にすることをお勧めいたします。
Ryotaro25/leetcode_first60#76 (comment)

result *= x;
}
x *= x;
n /= 2;
Copy link

Choose a reason for hiding this comment

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

自分なら n >>= 1 と書くのですが、趣味の範囲だと思います。

}
return result;
}
};

Choose a reason for hiding this comment

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

良いと思いました。
n と x を動かさず、別の変数を置いて while 文を回すのもありですね(趣味の範囲だと思います)。

Copy link

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

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

LGTM です

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.

6 participants