4

My task is to write a program which prompts the user to enter a positive double a and an integer n greater than 2, and print the value of the nth root of positive integer a to the screen with accuracy to 100 places. I've used Math.pow to be able to get the root, and I feel as though I've done everything right. The only problem is that every time I run my program, the output is 1.0, no matter what numbers I input for a and n. What is the problem with my code?

import java.util.Scanner;
import java.lang.Math;

public class Q8 {

    public static void main(String[] args) {

    System.out.println("Enter a positive number: ");
    Scanner in = new Scanner(System.in);
    double a = in.nextDouble();

    System.out.println("Enter an integer greater than 2: ");
    Scanner in2 = new Scanner(System.in);
    int n = in.nextInt();

    System.out.println(pow (a,n));
    }

private static double pow(double a, int n) {
      if (n == 0){
            return 1;
      }

      else{
         double sum = Math.pow(a,(1/n));
         return sum;
}

Why is the answer always 1.0?

Eran
  • 387,369
  • 54
  • 702
  • 768
Ibrewster
  • 187
  • 5
  • 18
  • 2
    I don't see `root` defined anywhere used in this expression: `Math.pow(a,(1/root))`. What is it? – icza Nov 10 '14 at 08:54
  • @icza It's a typo. Either `root` should be `n` or `n` should be `root`. – Dawood ibn Kareem Nov 10 '14 at 08:56
  • 2
    You're never going to get 100 decimal places using `double`. May I interest you in a `BigDecimal`? – Dawood ibn Kareem Nov 10 '14 at 08:59
  • `pow` is a terrible name for the function by the way. – weston Nov 10 '14 at 08:59
  • how would I incorporate BigDecimal? – Ibrewster Nov 10 '14 at 09:00
  • I'm just looking into it now. It looks like the JDK doesn't give you a way of doing it (as far as I can find). You'd have to either use some kind of mathematics library, or write your own implementation of one of the classic algorithms. The Newton Raphson method should do it, but it's going to require quite a lot of coding. – Dawood ibn Kareem Nov 10 '14 at 09:02
  • I don't think the logic for `n == 0` is correct. Besides `n` is meant to be positive according to the question. – weston Nov 10 '14 at 09:03
  • I can't find a way of doing exponents with `BigDecimal` or anything equivalent to it in either Guava or Apache. I can't believe this is so hard to find. Perhaps that's why your teacher has asked you to look into this. – Dawood ibn Kareem Nov 10 '14 at 09:08
  • Is it homework or a programming challenge like project euler? – weston Nov 10 '14 at 09:10

2 Answers2

8

Replace 1/n with 1.0/n.

You're getting integer division, so no matter what n is, if it's 2 or higher, then 1/n is coming out zero. Then you're raising your number to the zeroeth power, which gives 1.

Replacing 1 with 1.0 makes the division into a floating point division - that is, the result won't be truncated to an integer. This is what you want.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
5

First of all, I'm assuming that

double sum = Math.pow(a,(1/root));

should be

double sum = Math.pow(a,(1/n));

since there is no root variable in your code.

Second of all, 1/n would give you 0 for every integer n > 1. Therefore sum would be 1.0. You should replace it with :

double sum = Math.pow(a,(1.0/n));

or

double sum = Math.pow(a,(1/(double)n));

In order to get a division of double variables.

Eran
  • 387,369
  • 54
  • 702
  • 768