nth root implementation

后端 未结 8 1847
说谎
说谎 2020-12-30 01:16

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 s

8条回答
  •  清歌不尽
    2020-12-30 02:07

        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.

提交回复
热议问题