-2

I've been writing a few methods to treat a LinkedList in Java as an infinite array for scientific notation. The first index is the integer, and the rest are decimals. I've been working on a division algorithm between two LinkedLists, but have had a lot of trouble. Right now, my code goes through an infinite loop. If somebody could please help me find the issue (I don't need a solution written for me xD), I would be nearly finished. Assume that other methods I wrote are correct.

public static LinkedList<Integer> divide(LinkedList<Integer> a, LinkedList<Integer> b) {
    LinkedList<Integer> arr = new LinkedList<Integer>();
    LinkedList<Integer> base = new LinkedList<Integer>();
    base.add(a.get(0));
    boolean incomplete = true;
    int i = 0;

    while (incomplete) {
        LinkedList<Integer> multiplier = new LinkedList<Integer>();
        multiplier.add(1);
        while (LinkedListMethods.lessThan(LinkedListMethods.multiply(b, multiplier), base)) {
            multiplier.set(0, multiplier.get(0) + 1);
        }
        multiplier.set(0, multiplier.get(0) - 1);

        LinkedList<Integer> multiple = new LinkedList<Integer>();
        multiple = LinkedListMethods.multiply(b, multiplier);
        base = LinkedListMethods.subtract(base, multiple);

        arr.add(multiplier.get(0));

        base = LinkedListMethods.clean(base);

        if (i == a.size() - 1 && base.get(0) == 0) {
            incomplete = false;
        } else if (i == arr.size() - 1) {
            a.add(0);
        }

        i++;
    }

    return clean(arr);
}

Thank you for any help!

Edit 1: lessThan(a, b) returns true if a < b (it's tested to work)

Edit 2: The other methods are up at http://javaman.net/code/larger-decimals (it's compact and unreadable for some reason)

Gabriel Pizarro
  • 400
  • 4
  • 15
  • 3
    (1) It's hard to help if you don't provide all the code (what is `lessThan`? etc. See [mcve].(2) You should use a debugger and run your code step by step to understand the problem. – assylias Oct 30 '17 at 13:01
  • @QBrute The last `if` contains the `incomplete = false`, which probably never gets entered – SomeJavaGuy Oct 30 '17 at 13:11
  • @SomeJavaGuy Ah you're right. There it is. I'll delete my comment – QBrute Oct 30 '17 at 13:12

1 Answers1

1
while (incomplete) {
        LinkedList<Integer> multiplier = new LinkedList<Integer>();
        multiplier.add(1);
        while (LinkedListMethods.lessThan(LinkedListMethods.multiply(b, multiplier), base)) {
            multiplier.set(0, multiplier.get(0) + 1);
        }

You never update the value of incomplete within the While loop, therefore it will always be true. You need to update the value somehow or restructure your code.

Maximum size of HashSet, Vector, LinkedList

Has some information on the upper pratical size of linked lists. Even if in theory they have no upper limit, at some point you'll reach the limits of maximum value an Integer can have.