タイトル: ceil
SEOタイトル: Python math.ceil 完全ガイド(floor との対、整数引数、Decimal、numpy 連携)
| この記事の要点 |
|
基本: math.ceil の動作
import math
# 切り上げ(引数以上の最小の整数)
math.ceil(3.0) # 3
math.ceil(3.1) # 4
math.ceil(3.9) # 4
math.ceil(4.0) # 4
# 負数(0 に近い方向に切り上げ)
math.ceil(-3.1) # -3 ← -4 ではない
math.ceil(-3.9) # -3
math.ceil(-3.0) # -3
# 整数引数はそのまま
math.ceil(5) # 5(int)
# 戻り値は Python 3 で int(Python 2 は float)
type(math.ceil(3.5)) #
floor / round / trunc との違い
| 関数 | 3.2 | 3.5 | 3.8 | -3.2 | -3.5 | -3.8 | 動作 |
|---|---|---|---|---|---|---|---|
math.ceil(x) | 4 | 4 | 4 | -3 | -3 | -3 | 切り上げ(正の無限大方向) |
math.floor(x) | 3 | 3 | 3 | -4 | -4 | -4 | 切り下げ(負の無限大方向) |
math.trunc(x) | 3 | 3 | 3 | -3 | -3 | -3 | 小数部切り捨て(0 方向) |
round(x) | 3 | 4 | 4 | -3 | -4 | -4 | 偶数丸め(Banker's Rounding) |
int(x) | 3 | 3 | 3 | -3 | -3 | -3 | 0 方向(trunc と同じ) |
import math
# round は「偶数への銀行家丸め」なので注意
round(0.5) # 0 ← 1 ではない!
round(1.5) # 2
round(2.5) # 2 ← 3 ではない!
round(3.5) # 4
# 通常の四捨五入が欲しいなら:
def round_half_up(x):
return math.floor(x + 0.5)
round_half_up(0.5) # 1
round_half_up(2.5) # 3
典型的なユースケース
ページング: 総件数からページ数を計算
import math
total = 247 # 総件数
per_page = 20
# 13 ページ必要 → 切り上げ
page_count = math.ceil(total / per_page) # 13
# // 演算子では 12 になってしまう
total // per_page # 12 ← 端数が抜ける
# トリック: 整数演算で同じ結果
page_count = (total + per_page - 1) // per_page # 13
金額: 100 円単位で切り上げ
import math
price = 1234.5
# 1 円単位で切り上げ
math.ceil(price) # 1235
# 100 円単位で切り上げ
math.ceil(price / 100) * 100 # 1300
# 1000 円単位で切り上げ
math.ceil(price / 1000) * 1000 # 2000
Decimal 利用時の正確な切り上げ
float は 2 進数誤差があるため金額計算では Decimal を使うべきです:
from decimal import Decimal, ROUND_CEILING, ROUND_FLOOR, ROUND_HALF_UP
import math
# ❌ float の誤差
math.ceil(0.1 + 0.2) # 1
math.ceil(2.675 * 100) / 100 # 2.68 のはずが 2.68
# ✅ Decimal で正確
price = Decimal('1234.501')
# 小数 2 桁で切り上げ
rounded = price.quantize(Decimal('0.01'), rounding=ROUND_CEILING)
# Decimal('1234.51')
# 整数に切り上げ
rounded = price.quantize(Decimal('1'), rounding=ROUND_CEILING)
# Decimal('1235')
# 四捨五入(ROUND_HALF_UP)
Decimal('2.5').quantize(Decimal('1'), rounding=ROUND_HALF_UP)
# Decimal('3') ← 偶数丸めではない
NumPy の ceil
import numpy as np
arr = np.array([1.2, 2.5, 3.8, -1.7])
np.ceil(arr)
# array([ 2., 3., 4., -1.])
# 戻り値は float の ndarray(int 化したいなら astype)
np.ceil(arr).astype(int)
# array([ 2, 3, 4, -1])
# DataFrame でも同様
import pandas as pd
df = pd.DataFrame({'value': [1.2, 2.5, 3.8]})
df['ceil'] = np.ceil(df['value'])
他言語との対比
| 言語 | 関数 | 戻り値型 |
|---|---|---|
| Python | math.ceil(x) | int |
| JavaScript | Math.ceil(x) | Number |
| PHP | ceil($x) | float |
| Java | Math.ceil(double x) | double |
| C / C++ | ceil(double x) / ceill / ceilf | double 等 |
| Ruby | 3.2.ceil | Integer |
| Go | math.Ceil(x float64) | float64 |
// JavaScript(参考)
Math.ceil(3.2); // 4
Math.ceil(-3.2); // -3 ← Python と同じ挙動
// Math.round は「四捨五入」だが負数で挙動が違う
Math.round(0.5); // 1
Math.round(-0.5); // 0 ← -1 ではない
FAQ
Q: math.ceil(3.5) は 3 になるの 4 になるの?
A: 4 です。「以上の最小の整数」なので、3.5 以上で最小は 4。
Q: 負数の math.ceil(-3.5) はなぜ -3?
A: 「-3.5 以上の最小の整数」だから。数直線上で -3.5 から右(正の方向)に進んだ最初の整数 -3 が答え。
Q: math.ceil と -(-x // 1) はどちらが速い?
A: 大量データなら NumPy が圧倒的に速い。スカラーなら math.ceil が分かりやすく、CPython で C 実装なので十分高速です。