Tuesday, December 19, 2017

C++11: std::cbrt()

C++11 added the std::cbrt() function. It computes the cube root. Here is an example:

#include <cmath>
#include <iostream>
using namespace std;

int main()
{
  cout << cbrt(  1.0) << " "; //  1
  cout << cbrt(  8.0) << " "; //  2
  cout << cbrt( 27.0) << " "; //  3
  cout << cbrt( 64.0) << " "; //  4
  cout << cbrt(125.0) << " "; //  5 
  cout << cbrt(216.0) << " "; //  6 
  cout << cbrt(  2.0) << " "; //  1.25992
  cout << cbrt(  3.0) << " "; //  1.44225

  cout << endl;
  return 0;
}
// Output: 1 2 3 4 5 6 1.25992 1.44225
Reference: http://en.cppreference.com/w/cpp/numeric/math/cbrt

No comments:

Post a Comment