🦨

float16, float32, float64 変換時の丸め (python)

2023/11/16に公開

float16: 整数部分を含め,5桁まで保持  (例: 123.44)
float32: 整数部分を含め,8桁まで保持  (例: 123.45679)
float64: 整数部分を含め,17桁まで保持  (例: 123.45678912345679)

test.py
import numpy as np

### float16, 32, 64
a = np.array(123.45678912345678912)
b = np.array(123.45678912345678912)
c = np.array(123.45678912345678912)

a = a.astype(np.float16)
b = b.astype(np.float32)
c = c.astype(np.float64)

print(a) # 123.44
print(b) # 123.45679
print(c) # 123.45678912345679

Discussion