mathモジュールのsqrtメソッド
<mathモジュールの<sqrtメソッドは、
パラメータで指定された数値の平方根を返します。
常に実数値が返されます。
構文
pythonimport math
math.sqrt(数値)
例
数値<25の平方根を求めてみましょう:
pythonimport math
print(math.sqrt(25))
コード実行結果:
python5.0
例
次に、<10の平方根を取得してみましょう:
pythonimport math
print(math.sqrt(10))
コード実行結果:
python3.1622776601683795
例
負の数の平方根を取得してみましょう:
pythonimport math
print(math.sqrt(-100))
コードを実行すると、エラーが表示されます:
errorValueError: math domain error