pow() べき乗演算
最終更新:2026-09-22
pow() べき乗演算
pow(base, exp[, mod]) は base の exp 乗を返す。3 引数版は剰余べき乗演算をサポートする(より効率的)。
| カテゴリ | 組み込み関数 |
|---|---|
| 種類 | 組み込み関数 |
| Python バージョン | all |
📝 構文
pow(base, exp[, mod])
⚙️ 引数
| base 必須 | The base (a numeric type). |
|---|---|
| exp 必須 | The exponent (a numeric type). |
| mod 任意 | Modulus; when given, computes base**exp % mod efficiently.(デフォルト値:None) |
戻り値:base**exp, or (base**exp) % mod when mod is given.
💥 例外
TypeError— Raised when the operand types do not support exponentiation (e.g. strings).ValueError— Raised when base is negative, exp is negative and mod is given.OverflowError— Raised when the result is too large to compute (memory limit).
▶ 例
下のコードを編集し「実行」をクリックすると、出力パネルに結果が表示されます:
実行結果:
1024
0.125
1
🏷️ 関連する組み込み関数
💡 関連するケース
さらに多くのケースは近日公開予定です。
❓ よくある質問
Qpow(2, 10) と 2 ** 10 は同じですか?
A結果は同じ(1024)です。ただし ** には3引数形式がありません。pow(base, exp, mod) は高速な冪剰余で (base**exp) % mod を計算し、大きな数では明らかに効率的です。
Qpow(2, -3) は何を返しますか?
A0.125 を返します。負の指数は浮動小数点の逆数を返し、1 / (2**3) と等価です。
Qpow(3, 4, 5) はどう計算されますか?
Aまず 3**4 = 81 を計算し、次に 5 で剰余をとって 1 にします。81 % 5 と等価ですが、3引数形式は巨大な中間結果を作らないため、RSA など大きな数の冪剰余に適しています。
Qpow(-2, -3, 5) が ValueError になるのはなぜですか?
A負の底と負の指数の組み合わせでは、剰余演算で一意な結果を定義できないため、Python は直接 ValueError を投げます。負の底・負の指数・mod を同時に使うのは避けましょう。