Python math.remainder() メソッド

Python math モジュール

Python mathremainder(x, y)メソッドは x/y の余りを返します。

Python バージョン:3.7

構文

math.remainder() メソッドの構文は次のとおりです:

math.remainder(x, y)

引数の説明:

  • x-- 必須、被除数。
  • y-- オプション、除数。ゼロ以外の数でなければなりません。そうでない場合は ValueError が発生します。

戻り値

余りを表す浮動小数点値を返します。

例

以下の例では余りを計算します:

例

# math モジュールをインポート
import math

# x/y の余り
print (math.remainder(9, 2))
print (math.remainder(9, 3))
print (math.remainder(18, 4))

print (math.remainder(23.5, 5))
print (math.remainder(23, 5.5))
print (math.remainder(12.5, 2.5))
print (math.remainder(12, 2))

出力結果:

1.0
0.0
2.0
-1.5
1.0
0.0
0.0

Python math モジュール

その他の拡張