How can I obtain the cube root in C++? -


i know how obtain square root of number using sqrt function.

how can obtain cube root of number?

sqrt stands "square root", , "square root" means raising power of 1/2. there no such thing "square root root 2", or "square root root 3". other roots, change first word; in case, seeking how perform cube rooting.

before c++11, there no specific function this, can go first principles:

  • square root: std::pow(n, 1/2.) (or std::sqrt(n))
  • cube root: std::pow(n, 1/3.) (or std::cbrt(n) since c++11)
  • fourth root: std::pow(n, 1/4.)
  • etc.

if you're expecting pass negative values n, avoid std::pow solution — it doesn't support negative inputs fractional exponents, , why std::cbrt added:

std::cout << std::pow(-8, 1/3.) << '\n';  // output: -nan std::cout << std::cbrt(-8)      << '\n';  // output: -2 

n.b. . important, because otherwise 1/3 uses integer division , results in 0.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -