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?

More than 1 year has passed since last update.

numpy の 型昇格(type promotion) がやばい話

Posted at

結論: numpy ではちゃんとオーバーフローのめんどうを見ましょう

  • np.__version__ == '1.23.4'
  • 表は index * column の計算結果
  • スカラーと ndarray で挙動が違う
  • オーバーフローしたときの RuntimeWarning はスカラー同士の時にしか出ない
  • numpy.result_type の結果が計算結果の dtype 、足し算と掛け算で同じ型を使っているため最悪死ぬ
= i * j 255 np.uint8(255) np.uint8([255]) 257 np.uint16(257) np.uint16([257]) 258 np.uint16(258) np.uint16([258])
255 65025
int
65025
np.int32
[1]
np.ndarray(np.uint8)
65535
int
65535
np.int32
[65535]
np.ndarray(np.uint16)
65790
int
65790
np.int32
[254]
np.ndarray(np.uint16)
np.uint8(255) 65025
np.int32
1
np.uint8
[1]
np.ndarray(np.uint8)
65535
np.int32
65535
np.uint16
[65535]
np.ndarray(np.uint16)
65790
np.int32
254
np.uint16
[254]
np.ndarray(np.uint16)
np.uint8([255]) [1]
np.ndarray(np.uint8)
[1]
np.ndarray(np.uint8)
[1]
np.ndarray(np.uint8)
[65535]
np.ndarray(np.uint16)
[65535]
np.ndarray(np.uint16)
[65535]
np.ndarray(np.uint16)
[254]
np.ndarray(np.uint16)
[254]
np.ndarray(np.uint16)
[254]
np.ndarray(np.uint16)
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?