Will
Will

Reputation: 20235

nth root implementation

I am working on a way to calculate the nth root of a number. However, I am having problems with the nth root of negative numbers.

Most people say to use Math.pow(num, 1 / root), but this does not work for negative numbers.

I have tried this:

public static double root(double num, double root) {
    if (num < 0) {
        return -Math.pow(Math.abs(num), (1 / root));
    }
    return Math.pow(num, 1.0 / root);
}

but, it does not work for all numbers as the root can be a decimal. For example root(-26, 0.8) returns -58.71, but that is an invalid input. This will also give the wrong answer for even roots. For example root(-2, 2) returns -1.41421, but -2 does not have a square root.

Upvotes: 20

Views: 59287

Answers (8)

Pavel Kataykin
Pavel Kataykin

Reputation: 1557

I use the method below. Maybe it's not the most accurate, but it works well in my case.

public double root(double num, double root) {
    double d = Math.pow(num, 1.0 / root);
    long rounded = Math.round(d);
    return Math.abs(rounded - d) < 0.00000000000001 ? rounded : d;
}

Upvotes: 2

RandomHighShooler
RandomHighShooler

Reputation: 1

I'm not too sure about the exact code, but add an extra if statement to clarify between odd and even roots. something along the lines of

public static double root(double num, double root) {
    if (num < 0) {
        if(root%2==1) {
            return -Math.pow(Math.abs(num), (1 / root));
        }
    }
    return Math.pow(num, 1.0 / root);
}

Not entirely sure if this will work with your other code, but I hope it can help

Upvotes: 0

Rohit Kumar
Rohit Kumar

Reputation: 13

System.out.println( Math.pow(10, Math.log10(Number)/root));

Upvotes: -2

Alexis Labat
Alexis Labat

Reputation: 11

    public double root(double num, double root) {
        double y=1;
        double x;
        while(Math.pow(x, root) != num) {
            if(Math.pow(x, root) > num) {
                x=x-y;
                y=y/10;
            } else {
                x=x+y;
            }
        }
        return x;
    }   

This should work fine for you, although it isn't compact it uses as little math functions as possible.

Upvotes: 0

user3312489
user3312489

Reputation: 11

You could do if(num < 0){ return Math.abs(Math.pow(num, 1 / root)) } Then just use ' + "i"' whenever stating the value. Or use the absolute value for equations and later factor in the positive/negative and i when needed. That's what worked for me.

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117569

(num) ^ (1/root) is similar to exp( (1/root) * log(num) ), so you can do it like:

public static double root(double num, double root)
{
    return Math.pow(Math.E, Math.log(num)/root);
} 

Upvotes: 19

QuantumMechanic
QuantumMechanic

Reputation: 13946

What are you trying to do? Unless you're planning to fully and properly handle complex numbers you cannot take the nth root of a negative number.

For example, while (-8)^(1/3) has a principal branch of -2, the only branches of (-4)^(1/2) are 2i and -2i.

To handle this properly you need to transform the number into its polar form and then take the required root in that form.

So -8 is the complex number 8*exp(i*pi). The 1/3 roots of that are 2*exp(i*pi/3), 2*exp(i*pi), and 2*exp[i*(-pi)/3]. Then you can use de Moivre' formula to compute the roots in the form a + bi.

Upvotes: 10

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

Either use one of the many complex number packages available on the Internet, or convert your number to a rectangular position on the Argand plane, rotate it the appropriate angle as given by the root, then read it out as a (real, imaginary) pair.

Upvotes: 2

Related Questions