cat articles/numpy-cast

NumPy cast overflow behavior can vary by environment and array size

I got caught by exactly what the title says: overflow behavior when casting in NumPy differed depending on the environment and the size of the data. It seems to take a different code path depending on the array length, and it took me a while to identify the cause. The correct answer is probably "do not pass overflowing data into a cast", but if behavior changes like this, I would at least appreciate a warning that an overflow happened.

This feels like the kind of bug that can look fine during development on a Mac, while already being broken, and then behave differently in production. I did not dig far enough to know whether this is specific to the Mac environment or whether it depends on the BLAS implementation, such as Intel MKL.

import platform
print(platform.system())
# オーバーフローして1になる
print(np.array([257.0], dtype="float32").astype('uint8'))
# オーバーフローして1になる
print(np.array([257.0, 0, 0, 0, 0, 0, 0], dtype="float32").astype('uint8'))
# オーバーフローして1になるが正しい、と思いきや、環境によっては丸め込まれて255になる
print(np.array([257.0, 0, 0, 0, 0, 0, 0, 0], dtype="float32").astype('uint8'))
Linux
[1]
[1 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0]
Windows
[1]
[1 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0]

On my Intel Mac, the value is clamped instead.

Darwin
[1]
[1 0 0 0 0 0 0]
[255   0   0   0   0   0   0   0]

cat related_articles/numpy-cast.yaml

  1. Adding type hints to a Python project and getting value from type checkingAdding Python type hints and pyright to a machine learning project, with a low setup cost and immediately useful editor support and static checks.
  2. Understanding LangChain Expression Language (LCEL)LCEL is LangChain's recommended way to build chains. This article explains the basic behavior of Runnable, RunnableSequence, RunnableParallel, dict syntax, invoke, and RunnablePassthrough step by step.
  3. Solo silver medal, 43rd place, in Kaggle Feedback Prize - Predicting Effective ArgumentsA solo 43rd-place silver-medal solution for Feedback Prize - Predicting Effective Arguments, covering what worked, what failed, and the experience of competing alone.