Skip to content
Related Articles

Related Articles

Improve Article

sqrt, sqrtl and sqrtf in C++

  • Last Updated : 04 Nov, 2019
Geek Week

The cmath header defines two more inbuilt functions for calculating square root of a number (apart from sqrt which takes double as an argument) which has an argument of type float and long double.

  1. double sqrt(double arg): It returns the square root of a number to type double
    Syntax : double sqrt(double arg)
    




    // CPP code to illustrate
    // the use of sqrt function.
    #include <cmath>
    #include <iomanip>
    #include <iostream>
      
    using namespace std;
      
    int main()
    {
        double val1 = 225.0;
        double val2 = 300.0;
      
        cout << fixed << setprecision(12) << sqrt(val1) << endl;
        cout << fixed << setprecision(12) << sqrt(val2) << endl;
      
        return (0);
    }
    Output:
    15.000000000000
    17.320508075689
    

    Errors and exceptions associated with this function:
    (Thanks to sunny6041 for contributing this section)

    1. It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘sqrt()’ like this.




      #include <iostream>
      #include <cmath>
      using namespace std;
        
      int main()
      {
          double answer;
                
          answer = sqrt();
          cout << "Square root of " << a 
               << " is " << answer << endl;
        
          return 0;
      }

      Output:



      prog.cpp:9:16: error: no matching function for call to 'sqrt()'
      
    2. If we pass a negative value in the argument domain error occurs. and the output will be the Square root of -a is -nan.




      #include <iostream>
      #include <cmath>
      using namespace std;
        
      int main()
      {
          double a = -2 ,  answer;
                
          answer = sqrt(a);
          cout << "Square root of " << a 
               << " is " << answer << endl;
        
          return 0;
      }

      Output:

      Square root of -2 is -nan
      
      
  2. float sqrtf(float arg): It returns the square root of a number to type float
    Syntax : float sqrtf(float arg)
    




    // CPP code to illustrate
    // the use of sqrtf function.
    #include <cmath>
    #include <iomanip>
    #include <iostream>
      
    using namespace std;
      
    int main()
    {
        float val1 = 225.0;
        float val2 = 300.0;
      
        cout << fixed << setprecision(12) << sqrtf(val1) << endl;
        cout << fixed << setprecision(12) << sqrtf(val2) << endl;
      
        return (0);
    }
    Output:
    15.000000000000
    17.320508956909
    
  3. long double sqrtl(long double arg): It returns the square root of a number to type long double with more precision. Advantage of using this function is that when working with integers of the order 1018, calculating its square root with sqrt function may give an incorrect answer due to precision errors as default functions in programming language works with floats/doubles. But this will always give an accurate answer.
    Syntax : long double sqrtl(long double arg)
    

    An illustration given below shows the exact difference when working with long integers with sqrt and sqrtl.
    Using sqrt function.




    // CPP code to illustrate
    // the incorrectness of sqrt function.
    #include <cmath>
    #include <iomanip>
    #include <iostream>
      
    using namespace std;
      
    int main()
    {
        long long int val1 = 1000000000000000000;
        long long int val2 = 999999999999999999;
      
        cout << fixed << setprecision(12) << sqrt(val1) << endl;
        cout << fixed << setprecision(12) << sqrt(val2) << endl;
      
        return (0);
    }
    Output:
    1000000000.000000000000
    1000000000.000000000000
    

    Output:

    1000000000.000000000000
    1000000000.000000000000
    

    Using sqrtl function




    // CPP code to illustrate
    // the correctness of sqrtl function.
    #include <cmath>
    #include <iomanip>
    #include <iostream>
      
    using namespace std;
      
    int main()
    {
        long long int val1 = 1000000000000000000;
        long long int val2 = 999999999999999999;
      
        cout << fixed << setprecision(12) << sqrtl(val1) << endl;
        cout << fixed << setprecision(12) << sqrtl(val2) << endl;
      
        return (0);
    }
    Output:
    1000000000.000000000000
    999999999.999999999476
    
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :