0

I'm trying to do a BigInteger exponent calculation. For my calculation, I am trying to subtract m1 from m2 (both are BigInteger) and get a positive value, but if I convert this positive number to an int number, I end up getting a negative value.

BigInteger m1 = new BigInteger("2905012217");
BigInteger m2 = new BigInteger("534500746");
int exp = m2.subtract(m1).abs().intValue();
System.out.println(exp);

BigInteger g1 = new BigInteger("2");
BigInteger output_test_g1 = g1.pow(exp); 

output:

m1:2905012217
m2:534500746
exp:-1924455825

Error:

Exception in thread "main" java.lang.ArithmeticException: Negative exponent
    at java.base/java.math.BigInteger.pow(BigInteger.java:2401)
    at FHEv1.homomorphicEqualityTest(FHEv1.java:195)
    at FHEv1.main(FHEv1.java:323)
Pshemo
  • 122,468
  • 25
  • 185
  • 269
randomUser
  • 61
  • 1
  • 8

1 Answers1

0

Integer overflow. How does Java handle integer underflows and overflows and how would you check for it?

Too high and overflow to Integer.MIN_VALUE and continue counting if nessecary. Too low and go to Integer.MAX_VALUE and continue counting if nessecary.

For example,

int A = Integer.MAX_VALUE;
A++;
//A should be Integer.MIN_VALUE, correct me if I am wrong
FailingCoder
  • 757
  • 1
  • 8
  • 20