1

I am a novice programmer, just learning how to use recursions. Sorry if my question is very basic, but I wish to know why my code outputs extra values. I have run the code multiple times, and it outputs extra values. The point of the code is to take "Pi" and take the digits and "x2" it. So, 3.1415 = 6 2 8 2 10. The output that I got is below, thanks for your help!

public class printPi
{
static int x = 1;
static double pi = .314159265359;
public static void main(String[] args)
{   
    piPrinter(pi);
}
public static void piPrinter(double pi01)
{

    if(x!=0)
    {
        pi = pi*10;
        x = (int)(pi%10);
        x=x*2;
        System.out.println(x);
        piPrinter(pi);
    }
    else
    System.out.println("done.");

}

}

My Output:

6
2
8
2
10
18
4
12
10
6
10
16
18
18
18
18
12
12
12
0

done.(With new lines between each #)

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – shmosel Nov 15 '16 at 04:05
  • Your code correctly does what you say, namely doubling each digit in Pi. The problem is that multiplying pi by two doesn't mean just doubling each digit, because the overflow from each position needs to be forwarded to the higher tens place. Your code doesn't do this. – Tim Biegeleisen Nov 15 '16 at 04:07
  • Your code works as you expect. Just change the value of pi to .31415 – Denis Nov 15 '16 at 04:19
  • +denis, it has to work for the exact value that i gave – Ansh Shrivastava Nov 15 '16 at 04:27
  • As a novice programmer I suggest getting familiar with you IDE's debugger. The debugger will allow you to step through each line of code and visualize what each variable is set to at each point. This will help you deduce where you have made your mistakes. It is a very powerful tool once you figure out how to effectively use it – jrmullen Nov 15 '16 at 04:36
  • Hi, Mr. Mullen, I am using Text Wrangler. (I Love using eclipse, but as a HS Student, we are forced to use Text Wrangler) Is there a "IDE Debugger", like you are saying in Text Wrangler? – Ansh Shrivastava Nov 15 '16 at 04:42
  • Sorry Tim, I am not sure what you mean. You said that I multiply Pi by 2, but I thought that I am multiplying each digit by 2. Can you look in my code if I am wrong in that thought? – Ansh Shrivastava Nov 15 '16 at 04:45
  • Your pi is changing every loop. Just print it and u will see (that's why is calculating extra values) – Rcordoval Nov 15 '16 at 05:48
  • Oh, thank you so much Thrasher! – Ansh Shrivastava Nov 15 '16 at 06:25

2 Answers2

1

Try to evaluate only the digit at the left of the dot and "remove" it after the println.

like this:

public class printPi
{

    public static void main(String[] args)
    {   
        double pi = 3.14159265359;
        piPrinter(pi);
    }
    public static void piPrinter(double pi01)
    {

        if ((int) pi01 != 0) {

            System.out.println((int) pi01 * 2);
            piPrinter((pi01 - (int) pi01) * 10);

        } else {
            System.out.println("done.");
        }

    }
}
Dante
  • 106
  • 5
0

1) Your pi value is changing every time so I set a count to limit the iteration over the number.

2) Since you can't set precision to Double,(pi*=10) is not getting the right output, but you could use BigDecimal to this purpose.

piPrinter:

import java.math.BigDecimal;
import java.math.RoundingMode;

public class printPi
{
    static int x = 1;
    static double pi = .314159265359;
    static int piLength = Double.toString ( pi ).length ( ) -1 ;

    public static void main(String[] args)
    {   
        piPrinter(pi, 1);
    }
    public static void piPrinter(double pi01, int count)
    {
        if(count < piLength)
        {
            pi = pi*10;
            Double truncated = BigDecimal.valueOf(pi)
            .setScale(3, RoundingMode.HALF_UP)
            .doubleValue();        
            x = (int)(truncated%10);
            x=x*2;
            System.out.println ( x );
            piPrinter(pi, count+1);
        }
        else
            System.out.println("done.");

    }

}

I/O Examples:

static double pi = .314159265359;

6 2 8 2 10 18 4 12 10 6 10 18 done.

static double pi = .31415;

6 2 8 2 10 done.

Rcordoval
  • 1,932
  • 2
  • 19
  • 25
  • I'm so sorry, but is there a way to do it using the basic methods only? My java teacher gives an instant 0 to any code that he has not taught yet. Can you possibly show a simpler version? Thank you so much for your consideration! Note : This is not an assignment, but just a challenge problem he gave for fun. – Ansh Shrivastava Nov 15 '16 at 06:20
  • @Dante solution is pretty much simplier than mine. Stick with that. Good luck. – Rcordoval Nov 15 '16 at 14:19