0

"unsigned long int" keeps becoming 0. It has to be a really big integer that I can operate multiplication and modulo on. Example: 1234^123 % 1234

  • 3
    You need a big integer library as Jonny Henly said. Nevertheless, 1234^123%1234 acutally is 0. – chrizke Oct 19 '16 at 20:51
  • ^ is an xor operation in c++, not exponentiation, – doug Oct 19 '16 at 20:56
  • And watch precedence: `1234^123 % 1234` means this `1234 ^ (123 % 1234)` – doug Oct 19 '16 at 20:59
  • pow(a, b) % c can be computed using 32-bit math as long as a & c are less then 2^16. – brian beuning Oct 19 '16 at 21:01
  • @chrizke No, it's not 0, it's 1193. It's 0 if the op means ^ to be exponentiation – doug Oct 19 '16 at 21:04
  • @doug 1234 xor 123 does not generate a big integer. So – due to context and in my opinion – ^ is the exponential operator in the provided example. – chrizke Oct 19 '16 at 21:13
  • @chrizke It's also not zero even if ^ is overloaded to do exponentiation since you can't overload precedence. However, I agree with you about the probable intent and that would, of course, yield 0. – doug Oct 19 '16 at 21:20

2 Answers2

0

If you're using gcc you can try __uint128_t or maybe use double instead unless you need to do bitwise operations

tony petrov
  • 71
  • 1
  • 3
-2

Two roads you got
1. Implement what you need by learning how to do large calculations using integer arrays with maths tricks for speed up OR
2. Use a library

I would suggest to go with 2nd option if you are not tied to use external library. One such bigNum library is ttmath

OR If you have a option to switch to java, then java has builtin BigInteger, BigDecimal and more functionalities supported. Look at BigInteger javadoc

mtk
  • 13,221
  • 16
  • 72
  • 112