1

Since Python 3 there's no upper limit on the size of an int. I'd like to deal with huge ints of 150+ decimal digits.

This is far larger than a unsigned long long is guaranteed to be, so I don't think I can rely on PyLong_AsLongLong and manipulate that.

How can I perform mathematical operations on such large ints that get passed to my C extension's methods?

theonlygusti
  • 11,032
  • 11
  • 64
  • 119
  • 1
    Hmm... you [just built](https://codereview.stackexchange.com/q/257264/230375) ints with 1000000+ digits, and now you call 150+ digits "huge"? :-P – Manuel Mar 16 '21 at 23:55
  • @Manuel lmao, good to see you. I just pulled a random result from that actually, it was one of the smaller lists. – theonlygusti Mar 16 '21 at 23:57

1 Answers1

3

Just handle them as Python ints, with Python arithmetic operations like PyNumber_Add and PyNumber_Multiply. Don't try to convert to a machine integer.

Now, if you were hoping to gain any speed advantage by doing this in C, well, that's not going to happen. If you want to optimize large integer operations, try a more efficient bignum library, like gmpy2. Make sure your algorithm is efficient, too - that _reverseBits thing from your code review question is extremely inefficient regardless of the integer implementation used.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Thanks, I didn't know about the existence of all the PyNumber stuff. – theonlygusti Mar 16 '21 at 23:54
  • I'd say the algorithmic efficiency of their `_reverseBits` isn't that bad, given that they apparently only use it on tiny numbers. – Manuel Mar 17 '21 at 01:13
  • @Manuel: Yeah, on closer look, it looks like how they handle `bitstream` is a bigger issue. This algorithm really needs to work in terms of a bit sequence rather than bignums. – user2357112 Mar 17 '21 at 01:19