B
Bert Sierra
OK --
Most of the Java API makes sense to me, but there are aspects of the
java.math.BigInteger package that have me stumped. Any help would be
appreciated.
In one piece of code, I was trying to add two BigIntegers, as follows:
BigInteger a = ...something...
BigInteger b = ...something...
BigInteger c = a.add(b);
What I quickly discovered was that in some cases the result was returned
by destroying the value in a, and in other cases by destroying the value
in b. I couldn't see a pattern that would explain this behaviour, and
unfortunately in the code I was working with I needed consistent
behaviour.
The Sun API doc was fairly vague, claiming only that add(val) would
return a BigInteger whose value is (this + val) [see
<http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#add(ja
va.math.BigInteger)>]. It wouldn't explain any side effects of the
addition on the original arguments -- whether the addition would recycle
the objects used, or would result in a new BigInteger being created.
In the end, I had to augment my code to make copies of the original
BigIntegers prior to the addition, as shown below. I don't like the
fact that I'm creating additional objects, but it's necessary because I
can't figure out how add() [and subtract() and multiply()] operates:
BigInteger a = ...something...
BigInteger b = ...something...
BigInteger acopy = new BigInteger( a.toByteArray() );
BigInteger bcopy = new BigInteger( b.toByteArray() );
BigInteger c = acopy.add(bcopy);
Seems like an awful lot of work to safely add two integers!!
TIA for any advice....
Most of the Java API makes sense to me, but there are aspects of the
java.math.BigInteger package that have me stumped. Any help would be
appreciated.
In one piece of code, I was trying to add two BigIntegers, as follows:
BigInteger a = ...something...
BigInteger b = ...something...
BigInteger c = a.add(b);
What I quickly discovered was that in some cases the result was returned
by destroying the value in a, and in other cases by destroying the value
in b. I couldn't see a pattern that would explain this behaviour, and
unfortunately in the code I was working with I needed consistent
behaviour.
The Sun API doc was fairly vague, claiming only that add(val) would
return a BigInteger whose value is (this + val) [see
<http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#add(ja
va.math.BigInteger)>]. It wouldn't explain any side effects of the
addition on the original arguments -- whether the addition would recycle
the objects used, or would result in a new BigInteger being created.
In the end, I had to augment my code to make copies of the original
BigIntegers prior to the addition, as shown below. I don't like the
fact that I'm creating additional objects, but it's necessary because I
can't figure out how add() [and subtract() and multiply()] operates:
BigInteger a = ...something...
BigInteger b = ...something...
BigInteger acopy = new BigInteger( a.toByteArray() );
BigInteger bcopy = new BigInteger( b.toByteArray() );
BigInteger c = acopy.add(bcopy);
Seems like an awful lot of work to safely add two integers!!
TIA for any advice....