0

I am trying to implement code to convert decimal to binary with a certain precision, for that I use stack and linked list to add the non decimal and decimal parts calculated. I then use Stringbuilder to pop/poll element one by one to ge the final binary number. SOURCE : http://www.geeksforgeeks.org/convert-decimal-fraction-binary-number/

When I push the elements onto stack/list I see they are being pushed(Using o/p stmts for that). For some reason I dont see them when popping out the elements.

Here is my code

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryToDecimal {

public String toBinary(float n, int p){
    int non_dec = (int) Math.floor(n);
    Stack<Integer> s_non_dec = new Stack<>();
    LinkedList<Integer> q_dec = new LinkedList<>();
    float dec = n - non_dec;
    int quotient = 1;
    while(quotient > 0){
        quotient = non_dec/2;
        int remainder = non_dec%2;
        System.out.println("quotient"+quotient+"non_dec"+non_dec+"remainder"+remainder);
        s_non_dec.push(remainder);
        non_dec = quotient;
    }

    while(p>0){
        System.out.println("before dec"+dec);
        dec = dec*2;
        System.out.println("after dec"+dec);
        if(dec >=1){
            System.out.println("add 1");
            q_dec.add(1);
            dec = dec - 1;
        }
        else{
            System.out.println("add 0");
            q_dec.add(0);
        }
        p--;
    }
    StringBuilder sb = new StringBuilder();
    for(int i=0;i<s_non_dec.size();i++){
        System.out.println("pop"+s_non_dec.peek());
        sb.append(s_non_dec.pop());
    }
    sb.append('.');
    for(int i=0;i<q_dec.size();i++){
        System.out.println("poll"+q_dec.peek());
        sb.append(q_dec.poll());
    }

    return sb.toString();
}

public static void main (String args[]){
    BinaryToDecimal btd = new BinaryToDecimal();
    System.out.println(btd.toBinary(2.47f, 5));
}

}

My output :

quotient1non_dec2remainder0
quotient0non_dec1remainder1
before dec0.47000003
after dec0.94000006
add 0
before dec0.94000006
after dec1.8800001
add 1
before dec0.8800001
after dec1.7600002
add 1
before dec0.7600002
after dec1.5200005
add 1
before dec0.52000046
after dec1.0400009
add 1
pop1
poll0
poll1
poll1
1.011

as seen above, even tough I push 1 and 0 into my stack, my output has only 1 for the non decimal part instead of 1 and 0! The same happens for the decimal part! Ive been looking at this code for hours, any help is appreciated!

1 Answer 1

1

Error is with your for loop.

for(int i=0;i<s_non_dec.size();i++){
    System.out.println("pop"+s_non_dec.peek());
    sb.append(s_non_dec.pop());
}

Here you are looping on stack size s_non_dec.size, which will keep on decreasing after every pop operation and "i" will keep on increasing after every iteration. You can better check if stack is empty or not. Use

while(!s_non_dec.isEmpty()) {
    System.out.println("pop"+s_non_dec.peek());
    sb.append(s_non_dec.pop());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.