I have a for
loop with BigInteger
s, something like this:
for(BigInteger a = BigInteger.valueOf(1); a.compareTo(someBigInteger); ++a) {...
Obviously I can't use the ++
operator on a non-primitive. How do I work around this?
Also, I have to use BigInteger
in this scenario.
You can have it like this. for(BigInteger a = BigInteger. ONE; a. compareTo(someBigInteger); a=a.
math. BigInteger. add(BigInteger val) is used to calculate the Arithmetic sum of two BigIntegers. This method is used to find arithmetic addition of large numbers of range much greater than the range of biggest data type double of java without compromising with the precision of the result.
++a
is a prefix increment, not a postfix increment, but in the context of a for-loop it doesn't really matter, as you ignore the return value of that statement anyway. In any event, this functionality could be acheieved by calling BigInteger.add
. Also note that compareTo
returns an int
, and since Java does not have implicit casts between int
s and boolean
s (like, e.g., C does), you'd have to compare its result to 0 to see if a
is smaller, larger or equal to someBigInteger
):
for (BigInteger a = BigInteger.ONE;
a.compareTo(someBigInteger) < 0;
a = a.add(BigInteger.ONE)) {...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With