Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I get nth root of real numbers

    Dear Stata users,

    In Stata there's a function sqrt() to get the square root of x. Now I want to caculate the nth root of x, and is there any function to achieve this? Thank you.

  • #2
    Are you looking for something like this?
    Code:
    di exp(ln(16)/3)
    di 16^(1/3)
    Or are you looking for something like Mata's polyroots()?

    Comment


    • #3
      Keep in mind that if the former, then you'll need to separately keep track of the argument's sign.

      Comment


      • #4
        Joseph Coveney, thank you very much! That is exactly what I want.

        Comment


        • #5
          Odd integer roots are sometimes of interest and they are defined for negative arguments too. Cube roots are in my reading by far the most common example. Thus just as -8 is the cube of -2, so by the same principle -2 is the cube root of -8. But to get cube roots correctly in Stata you need something more like

          Code:
          sign(x) * (abs(x))^(1/3)
          The reason is that powering is usually implemented in terms of logarithms, for which arguments must be positive. (It is easy enough to trap zero arguments.)

          Comment


          • #6
            Nick Cox, thank you very much. And congratulate on your 34567 posts.
            Click image for larger version

Name:	_20241016222056.png
Views:	1
Size:	47.7 KB
ID:	1765863

            Comment


            • #7
              Ha! I like that. It is not prime as it is the product 13 x 2659.

              Comment

              Working...
              X