0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

先日、Pythonでmath関連の関数を使ったので、復習としてメモ。

mathの関数 : sqrt / pow / ceil / floor / fabs

math.sqrt(x)

xの平方根を返す

import math

x = 25
result = math.sqrt(x)
print(f"{x} = {result}")  # 出力: √25 = 5.0

math.pow(x, y)

xのy乗を返す

import math

x = 2
y = 3
result = math.pow(x, y)
print(f"{x}^{y} = {result}")  # 出力: 2^3 = 8.0

math.ceil(x)

x以上の最小の整数を返す(切り上げ)

import math

x = 3.2
result = math.ceil(x)
print(f"ceil({x}) = {result}")  # 出力: ceil(3.2) = 4

math.floor(x)

x以下の最大の整数を返す(切り捨て)

import math

x = 3.8
result = math.floor(x)
print(f"floor({x}) = {result}")  # 出力: floor(3.8) = 3

math.fabs(x)

xの絶対値を返す

import math

x = -5
result = math.fabs(x)
print(f"|{x}| = {result}")  # 出力: |-5| = 5.0

まとめ

今回は、mathモジュールの関数について、5つ紹介しました。
他にもたくさんあるので、改めてご紹介します!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?