Python Forum
How to do "fixed size" (wrapping) math in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do "fixed size" (wrapping) math in Python?
#11
Okay, I have studied the Python implementation of FNV-1a (64-Bit) hash, which uses the unit64_t (64-bit unsigned integer) type in the "C" version:
https://de.wikipedia.org/wiki/FNV_(Infor...%C3%BCssel
https://en.wikipedia.org/wiki/Fowler%E2%...NV-1a_hash

Note the requirements:
Quote:All variables, except for byte_of_data, are 64-bit unsigned integers.
The multiply returns the lower 64-bits of the product.

In Python's implementation, they effectively do:
for char in text:
    n = (n ^ ord(char)) * prime
    n = n & mask
...with mask defined as 0xffffffffffff. I think the bit-wise AND with mask is done in order to get the "lower 64-bits of the product", as needed.

So, this is probably more efficient than "modulus" operation and I think I can adopt this technique for my needs.

Nonetheless, numpy looks interesting and I will definitely give it a try! However, in my test, multiplying two np.uint64 values produced an overflow warning Think
Reply
#12
Generally overflow is a bad thing. You can turn the warning ogf.
Reply
#13
I agree, that overflow is bad in general. But it is perfectly fine when doing "wrapping" math.

That is why, for example, in Rust the standard × and + operators throw an error on overflow, whereas the wrapping_add() or wrapping_mul() intrinsics do not.

Does Python/numpy have a similar thing? Turning off the warnings "globally" probably isn't a good idea...
Reply
#14
You turn off the warning for numpy, not Python.

https://numpy.org/doc/stable/reference/g...nings.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 326 Jan-14-2024, 03:56 AM
Last Post: 1418
  Wrapping c/c++ dll Oolongtea 2 737 Aug-25-2023, 10:35 PM
Last Post: PyDan
  Fixed colum width for rowLabels i Matplotlib pandabay 0 428 Jun-10-2023, 03:40 PM
Last Post: pandabay
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,492 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  Math python question Deonvek 6 1,163 Apr-05-2023, 09:27 PM
Last Post: deanhystad
  wrapping c++ library JESuh 2 1,313 Jun-16-2022, 08:18 AM
Last Post: JESuh
  math.log versus math.log10 stevendaprano 10 2,427 May-23-2022, 08:59 PM
Last Post: jefsummers
  Encrypt and decrypt in python using own fixed key SriRajesh 3 4,883 Feb-20-2022, 01:18 PM
Last Post: dboxall123
Question Opening small size browser with python selenium not work, need help greenpine 0 1,629 Feb-07-2022, 11:36 AM
Last Post: greenpine
  How to set Tab size to 4 in Python interpreter? zzzhhh 1 1,857 Jan-18-2022, 12:11 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020