nth root implementation

后端 未结 8 1827
说谎
说谎 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 01:47

    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.

提交回复
热议问题