Skip to content

Conversation

@hatute
Copy link

@hatute hatute commented Sep 18, 2024

since the n_events assigned with the UINT8 type of number "ne". So, the default type of n_events will also be UINT8. During the summation of all three byte of "ne" will potentially cause overflow. In my case, python raised overflow error when it reach 256 which exceed the upper limit of 255. Direct Bit-Wise operation can avoid this.

Reference issue (if any)

What does this implement/fix?

Additional information

since the n_events assigned with the UINT8 type of number "ne". So, the default type of n_events will also be UINT8. During the summation of all three byte of "ne" will potentially cause overflow. In my case, python raised overflow error when it reach 256 which exceed the upper limit of 255. Direct Bit-Wise operation can avoid this.
@welcome
Copy link

welcome bot commented Sep 18, 2024

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴

Comment on lines 1442 to +1443
ne = np.fromfile(fid, UINT8, 3)
n_events = ne[0]
for i in range(1, len(ne)):
n_events = n_events + ne[i] * 2 ** (i * 8)
n_events = ne[0] + (ne[1] << 8) + (ne[2] << 16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fix on NumPy < 2 but on 2.1 I get:

>>> np.uint8(255) + np.uint8(255)
<stdin>:1: RuntimeWarning: overflow encountered in scalar add
np.uint8(254)

So I think maybe doing this would be a clean way to fix it

ne = np.fromfile(fid, UINT8, 3).astype(np.int64)

or similar

@larsoner
Copy link
Member

I think this was fixed by #12909 in the meantime, but happy to see other PRs in the future!

@larsoner larsoner closed this Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants