Python math.fsum() メソッド

Python math 模块Python math モジュール

Python math.fsum(iterable)このメソッドは、イテラブルオブジェクト(タプル、配列、リストなど)内の要素の合計を計算します。

Python バージョン:2.6

構文

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

math.fsum(iterable)

パラメータの説明:

  • iterable-- 必須のイテラブルオブジェクト。リスト、タプル、配列を指定できます。イテラブルオブジェクトの要素が数値でない場合、TypeError を返します。

戻り値

イテラブルオブジェクト内のすべての要素の合計を表す浮動小数点値。

例

次の例では、すべての要素の合計を計算します:

例

# math パッケージをインポート
import math

# すべての要素の合計を計算
print(math.fsum([1, 2, 3, 4, 5]))
print(math.fsum([100, 400, 340, 500]))
print(math.fsum([1.7, 0.3, 1.5, 4.5]))

出力結果:

15.0
1340.0
8.0

Python math 模块Python math モジュール

その他の拡張