Complex numbers in C++ | Set 2

We introduced and discussed the concept in Complex numbers in C++ | Set 1

The remaining functions with example are discussed here:

  • log() – It is used to return the log of the complex number.
    // CPP program to illustrate the use of log()
    #include <iostream>     
     
    // for std::complex, std::log
    #include <complex> 
    using namespace std;
     
    // driver program
    int main ()
    {    
      // initializing the complex: (-1.0+0.0i)
      complex<double> mycomplex (-1.0, 0.0);
     
      // use of log()
      cout << "The log of " << mycomplex << " is "
           << log(mycomplex) <<endl;
     
      return 0;
    } 
    

    Output:

    The log of (-1,0) is (0,3.14159)
    
  • cos() – It computes complex cosine of a complex value z. Mathematical definition of the cosine is
    cos z = (e^(iz) + e^(-iz))/2
  • sin() – It computes the complex sine of a complex value z. Mathematical definition of the cosine is
     sin z = (e^(iz) - e^(-iz))/2i
  • tan() – It computes the complex tangent of a complex value z. Mathematical definition of the tangent is
    tan z = i(e^(-iz) - e^(iz)) / (e^(-iz) + e^iz)
    // example to illustrate the use of sin(), cos() and tan()
    #include <iostream>     
     
    // CPP program to illustrate
    // std::complex, std::cos, std::sin, std::tan
    #include <complex> 
    using namespace std;
     
    // driver program
    int main ()
    {    
      // initializing the complex: (-1.0+0.0i)
      complex<double> mycomplex (0.0, 1.0);
     
      // use of cos()
      cout << "The cos of " << mycomplex << " is "
           << cos(mycomplex) <<endl;
           
      // use of sin()
      cout << "The sin of " << mycomplex << " is "
           << sin(mycomplex) <<endl; 
           
      // use of tan()
      cout << "The tan of " << mycomplex << " is "
           << tan(mycomplex) <<endl; 
     
      return 0;
    }
    

    Output:

    The cos of (0,1) is (1.54308,-0)
    The sin of (0,1) is (0,1.1752)
    The tan of (0,1) is (0,0.761594)
    
  • cosh() – It finds the hyperolic cosine of the given complex. Mathematical function of hyperbolic cosine is:
    cosh(z)=(e^z+e^(-z))/2
  • sinh() – It finds the hyperbolic sine of the given complex. Mathematical function of hyperolic sine is:
      sinh(z)=(e^z-e^(-z))/2.
  • tanh() – It finds the hyperbolic tangent of the given complex.Mathematical function of hyperolic tan is:
    tanh(z)=(e^(2z)-1)/(e^(2z)+1)
    // CPP program to illustrate the 
    // use of cosh(),sinh(),tanh()
    #include <iostream>
    #include <cmath>
    
    // For std::complex
    #include <complex>
    using namespace std;
     
    // Driver program
    int main()
    {       
        // behaves like real cosh, sinh, tanh along the real line;
        // z = a + 0i
        complex<double> z(1, 0); 
        cout << "cosh" << z << " = " << cosh(z)
                  << " (cosh(1) = " << cosh(1) << ")"<<endl;
        cout << "sinh" << z << " = " << sinh(z)
                  << " (sinh(1) = " << sinh(1) << ")"<<endl;
        cout << "tanh" << z << " = " << tanh(z)
                  << " (tanh(1) = " << tanh(1) << ")"<<endl;
        
        // behaves like real cosine,sine,tangent along the imaginary line; z2=0+1i
        complex<double> z2(0, 1); 
        cout << "cosh" << z2 << " = " << cosh(z2)
                  << " ( cos(1) = " << cos(1) << ")"<<endl;
        cout << "sinh" << z2 << " = " << sinh(z2)
                  << " ( sin(1) = " << sin(1) << ")"<<endl;
        cout << "tanh" << z2 << " = " << tanh(z2)
                  << " ( tan(1) = " << tan(1) << ")"<<endl;
    }
    

    Output:

    cosh(1.000000,0.000000) = (1.543081,0.000000) (cosh(1) = 1.543081)
    sinh(1.000000,0.000000) = (1.175201,0.000000) (sinh(1) = 1.175201)
    tanh(1.000000,0.000000) = (0.761594,0.000000) (tanh(1) = 0.761594)
    cosh(0.000000,1.000000) = (0.540302,0.000000) ( cos(1) = 0.540302)
    sinh(0.000000,1.000000) = (0.000000,0.841471) ( sin(1) = 0.841471)
    tanh(0.000000,1.000000) = (0.000000,1.557408) ( tan(1) = 1.557408)
    

This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.