Help with BigInteger addition

Discussion in 'Java' started by Bert Sierra, Aug 25, 2004.

  1. Bert Sierra

    Bert Sierra Guest

    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....
    Bert Sierra, Aug 25, 2004
    #1
    1. Advertising

  2. Bert Sierra wrote:
    > 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.


    That's impossible unless your Java implementation is very broken.
    More likely, your code is broken.

    > 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.


    There is no vagueness. The very first word of the API doc for BigInteger
    is "Immutable". A BigInteger, once created, will not change, ever.
    So there *are no* side effects. There are, however, cases where the
    object returned will be one of the two objects that were added (if the
    other one is 0).

    Try to write a program as simple as possible that demonstrates this
    "destruction" of the arguments to the addition operation. You'll
    either find what part of your code is broken, find that you can't
    reproduce this behaviour outside the rest of your code (where there's
    something broken), or we'll be able to point out what your doing wrong
    immediately after seeing this program.

    Of course, all of this assumes that it is in fact not your Java
    implementation that's broken, but Sun's releases are very unlikely
    to contain such an obvious violation of the API contract.
    Michael Borgwardt, Aug 25, 2004
    #2
    1. Advertising

  3. Bert Sierra

    Rogan Dawes Guest

    Bert Sierra wrote:

    > 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.


    That shouldn't happen. BigIntegers are immutable - that is, it cannot be
    changed, just like normal Integer, or the other Number classes.

    >
    > 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.


    Yes, the return value of the various operations is a new BigInteger
    instance, that holds the result of the operation that was performed. The
    arguments (a and b) are not modified.
    >
    > 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!!
    >

    Seems to me like you are doing something wrong.

    > TIA for any advice....


    Check your code more carefully. You'll surely find your mistake.

    Rogan
    --
    Rogan Dawes

    *ALL* messages to will be dropped, and added
    to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
    Rogan Dawes, Aug 25, 2004
    #3
  4. Bert Sierra

    Paul Lutus Guest

    Bert Sierra wrote:

    > 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.


    Post examples of each case. Post short, compilable, working examples. Show
    the behavior you are saying exists.

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Aug 25, 2004
    #4
  5. Bert Sierra

    Bert Sierra Guest

    In article <>,
    Michael Borgwardt <> wrote:

    > Bert Sierra wrote:
    > > 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.

    >
    > That's impossible unless your Java implementation is very broken.
    > More likely, your code is broken.


    I took a closer look at my code, and you folks were right -- there was a
    problem in my code where I was incorrectly swapping references to two
    BigIntegers. To be sure, I wrote a simple piece of test code that spits
    out a Fibonacci sequence (below). In the first iteration of the loop, c
    is assigned the reference to b, since b and c are both equal to 1. In
    the second and subsequent iterations of the loop, c is assigned a
    references to new BigInteger objects: 2, 3, 5, 8, 13, and so on ad
    infinitum...


    import java.math.BigInteger;

    public class FibonacciSequence {
    public static void main (String args[]) {
    BigInteger a = BigInteger.ZERO;
    BigInteger b = BigInteger.ONE;

    while (true) {
    System.out.println(b);
    BigInteger c = a.add(b);
    a = b;
    b = c;
    }
    }
    }
    Bert Sierra, Aug 26, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pete kirkham
    Replies:
    5
    Views:
    450
    pete kirkham
    Aug 13, 2003
  2. xarax
    Replies:
    6
    Views:
    8,590
    xarax
    Sep 8, 2003
  3. nick
    Replies:
    0
    Views:
    885
  4. nick
    Replies:
    1
    Views:
    31,710
    Eric Sosman
    Oct 26, 2004
  5. Gandu
    Replies:
    1
    Views:
    527
    Michael Borgwardt
    Nov 29, 2004
Loading...

Share This Page