How to find arctangent with Examples

Last Updated : 15 Jul, 2025

What is arc tangent?

The arctangent is the inverse of the tangent function. It returns the angle whose tangent is the given number.

catan() is an inbuilt function in <complex.h> header file which returns the complex inverse tangent (or arc tangent) of any constant, which divides the imaginary axis on the basis of the inverse tangent in the closed interval [-i, +i] (where i stands for iota), used for evaluation of a complex object say z is on imaginary axis whereas to determine a complex object which is real or integer, then internally invokes pre-defined methods as:

S.No.

Method 

Return Type

1.

atan() function takes a complex z of datatype double which determine arc tangent for real complex numbersReturns complex arc tangent lies in a range along real axis [-PI/2, +PI/2] for an argument of type double.

2.

atanf() function takes a complex z of datatype float double which determine arc tangent for real complex numbers.Returns complex arc tangent lies in a range along real axis [-PI/2, +PI/2] for an argument of type float.

3.

atanl() function takes a complex z of datatype long double which determine arc tangent for real complex numbersReturns complex arc tangent lies in a range along real axis [-PI/2, +PI/2] for an argument of type long double.

4.

catan() function takes a complex z of datatype double which also allows imaginary part of complex numbersReturns complex arc tangent lies in a range along imaginary axis [-i, +i] for a complex object of type double

5.

catanf()  function takes a complex z of datatype float double which also allows imaginary part of complex numbersReturns complex arc tangent lies in a range along imaginary axis [-i, +i] for a complex object of type float

6.

catanl()  function takes a complex z of datatype long double which also allows imaginary part of complex numbersReturns complex arc tangent lies in a range along imaginary axis [-i, +i] for a complex object of type long double

Syntax:

atan(double arg);
atanf(float arg);
atanl(long double arg);
where arg is a floating-point value
catan(double complex z);
catanf(float complex z);
catanl( long double complex z);
where z is a Type – generic macro


Parameter: These functions accept one mandatory parameter z which specifies the inverse tangent. The parameter can be of double, float, or long double datatype.

Return Value: This function returns complex arc tangent/arc tangent according to the type of the argument passed.

Below are the programs illustrate the above method:

Program 1: This program will illustrate the functions atan(), atanf(), and atanl() computes the principal value of the arc tangent of floating – point argument. If a range error occurs due to underflow, the correct result after rounding off is returned. 

C++
// C++ program to illustrate the use
// of functions atan(), atanf(),
// and atanl()
#include<bits/stdc++.h>
using namespace std;

//Drive code
int main()
{
    // For function atan()
    cout << "atan(1) = " << atan(1) << ", ";
    cout << "4*atan(1) = " << 4 * atan(1) << "\n";

    cout << "atan(-0.0) = " << atan(-0.0) << ", ";
    cout << "atan(+0.0) = " << atan(0) << "\n";

    // For special values INFINITY
    cout << "atan(INFINITY) = " << atan(INFINITY) << ", ";
    cout << "2*atan(INFINITY) = " << 2 * atan(INFINITY) << "\n\n";

    // For function atanf()
    cout << "atanf(1.1) = " << atanf(1.1) << ", ";
    cout << "4*atanf(1.5) = " << 4 * atanf(1.5) << "\n";

    cout << "atanf(-0.3) = " << atanf(-0.3) << ", ";
    cout << "atanf(+0.3) = " << atanf(0.3) << "\n";

    // For special values INFINITY
    cout << "atanf(INFINITY) = " << atanf(INFINITY) << ", ";
    cout << "2*atanf(INFINITY) = " << 2 * atanf(INFINITY) << "\n\n";

    // For function atanl()
    cout << "atanl(1.1) = " << atanl(1.1) << ", ";
    cout << "4*atanl(1.7) = " << 4 * atanl(1.7) << "\n";

    cout << "atanl(-1.3) = " << atanl(-1.3) << ", ";
    cout << "atanl(+0.3) = " << atanl(0.3) << "\n";

    // For special values INFINITY
    cout << "atanl(INFINITY) = " << atanl(INFINITY) << ", ";
    cout << "2*atanl(INFINITY) = " << 2 * atanl(INFINITY) << "\n\n";

    return 0;
}
C
// C program to illustrate the use
// of functions atan(), atanf(),
// and atanl()
#include <math.h>
#include <stdio.h>

// Driver Code
int main()
{
    // For function atan()
    printf("atan(1) = %lf, ",
           atan(1));
    printf(" 4*atan(1)=%lf\n",
           4 * atan(1));

    printf("atan(-0.0) = %+lf, ",
           atan(-0.0));
    printf("atan(+0.0) = %+lf\n",
           atan(0));

    // For special values INFINITY
    printf("atan(Inf) = %lf, ",
           atan(INFINITY));
    printf("2*atan(Inf) = %lf\n\n",
           2 * atan(INFINITY));

    // For function atanf()
    printf("atanf(1.1) = %f, ",
           atanf(1.1));
    printf("4*atanf(1.5)=%f\n",
           4 * atanf(1.5));

    printf("atanf(-0.3) = %+f, ",
           atanf(-0.3));
    printf("atanf(+0.3) = %+f\n",
           atanf(0.3));

    // For special values INFINITY
    printf("atanf(Inf) = %f, ",
           atanf(INFINITY));
    printf("2*atanf(Inf) = %f\n\n",
           2 * atanf(INFINITY));

    // For function atanl()
    printf("atanl(1.1) = %Lf, ",
           atanl(1.1));
    printf("4*atanl(1.7)=%Lf\n",
           4 * atanl(1.7));

    printf("atanl(-1.3) = %+Lf, ",
           atanl(-1.3));
    printf("atanl(+0.3) = %+Lf\n",
           atanl(0.3));

    // For special values INFINITY
    printf("atanl(Inf) = %Lf, ",
           atanl(INFINITY));
    printf("2*atanl(Inf) = %Lf\n\n",
           2 * atanl(INFINITY));

    return 0;
}
Java
import java.text.DecimalFormat;

public class Main {

    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("#.#####"); // Pattern to round to five decimal places

        // For function atan()
        System.out.print("atan(1) = " + df.format(Math.atan(1)) + ", ");
        System.out.println("4 * atan(1) = " + df.format(4 * Math.atan(1)));

        System.out.print("atan(-0.0) = " + df.format(Math.atan(-0.0)) + ", ");
        System.out.println("atan(+0.0) = " + df.format(Math.atan(0)));

        // For special values INFINITY
        System.out.print("atan(INFINITY) = " +
                         df.format(Math.atan(Double.POSITIVE_INFINITY)) + ", ");
        System.out.println("2 * atan(INFINITY) = " + 
                           df.format(2 * Math.atan(Double.POSITIVE_INFINITY)) + "\n");

        // For function atanf()
        System.out.print("atanf(1.1) = " + df.format(Math.atan(1.1)) + ", ");
        System.out.println("4 * atanf(1.5) = " + df.format(4 * Math.atan(1.5)));

        System.out.print("atanf(-0.3) = " + df.format(Math.atan(-0.3)) + ", ");
        System.out.println("atanf(+0.3) = " + df.format(Math.atan(0.3)));

        // For special values INFINITY
        System.out.print("atanf(INFINITY) = " + 
                         df.format(Math.atan(Double.POSITIVE_INFINITY)) + ", ");
        System.out.println("2 * atanf(INFINITY) = " + 
                           df.format(2 * Math.atan(Double.POSITIVE_INFINITY)) + "\n");

        // For function atanl()
        System.out.print("atanl(1.1) = " + df.format(Math.atan(1.1)) + ", ");
        System.out.println("4 * atanl(1.7) = " + df.format(4 * Math.atan(1.7)));

        System.out.print("atanl(-1.3) = " + df.format(Math.atan(-1.3)) + ", ");
        System.out.println("atanl(+0.3) = " + df.format(Math.atan(0.3)));

        // For special values INFINITY
        System.out.print("atanl(INFINITY) = " +
                         df.format(Math.atan(Double.POSITIVE_INFINITY)) + ", ");
        System.out.println("2 * atanl(INFINITY) = " + 
                           df.format(2 * Math.atan(Double.POSITIVE_INFINITY)) + "\n");
    }
}
Python
import math

# For function atan()
print(f"atan(1) = {math.atan(1)}, ", end="")
print(f"4 * atan(1) = {4 * math.atan(1)}")
print(f"atan(-0.0) = {math.atan(-0.0)}, ", end="")
print(f"atan(0.0) = {math.atan(0)}")

# For special values INFINITY
print(f"atan(math.inf) = {math.atan(math.inf)}, ", end="")
print(f"2 * atan(math.inf) = {2 * math.atan(math.inf)}\n")

# For function atan2()
print(f"atan2(2, 1) = {math.atan2(2, 1)}, ", end="")
print(f"atan2(1, 1) = {math.atan2(1, 1)}")
print(f"atan2(-1, -1) = {math.atan2(-1, -1)}, ", end="")
print(f"atan2(0, 0) = {math.atan2(0, 0)}")

# For special values INFINITY
print(f"atan2(math.inf, 1) = {math.atan2(math.inf, 1)}, ", end="")
print(f"2 * atan2(math.inf, 1) = {2 * math.atan2(math.inf, 1)}\n")

# For function atan2l()
print(f"atan2(math.atan2l(2, 1), ", end="")
print(f"atan2(math.atan2l(1, 1))")
print(f"atan2(math.atan2l(-1, -1), ", end="")
print(f"atan2(math.atan2l(0, 0))")

# For special values INFINITY
print(f"atan2(math.atan2l(math.inf, 1), ", end="")
print(f"atan2(math.atan2l(math.inf, 1))")
C#
using System;

public class Program
{
    public static void Main(string[] args)
    {
        // For function Math.Atan()
        Console.WriteLine($"atan(1) = {Math.Atan(1):F6}, 4 * atan(1) = {4 * Math.Atan(1):F5}");

        Console.WriteLine($"atan(-0.0) = {Math.Atan(-0.0):F1}, atan(+0.0) = {Math.Atan(+0.0):F1}");

        // For special values double.PositiveInfinity
        Console.WriteLine($"atan(INFINITY) = {Math.Atan(double.PositiveInfinity):F4}, 2 * atan(INFINITY) = {2 * Math.Atan(double.PositiveInfinity):F5}\n");

        // For function MathF.Atan()
        Console.WriteLine($"atanf(1.1) = {MathF.Atan(1.1F):F6}, 4 * atanf(1.5) = {4 * MathF.Atan(1.5F):F5}");

        Console.WriteLine($"atanf(-0.3) = {MathF.Atan(-0.3F):F6}, atanf(+0.3) = {MathF.Atan(+0.3F):F6}");

        // For special values float.PositiveInfinity
        Console.WriteLine($"atanf(INFINITY) = {MathF.Atan(float.PositiveInfinity):F4}, 2 * atanf(INFINITY) = {2 * MathF.Atan(float.PositiveInfinity):F5}\n");

        // For function Math.Atan2()
        Console.WriteLine($"atanl(1.1) = {Math.Atan2(1.1, 2.2):F6}, 4 * atanl(1.7) = {4 * Math.Atan2(1.7, 2.2):F5}");

        Console.WriteLine($"atanl(-1.3) = {Math.Atan2(-1.3, 0.3):F6}, atanl(+0.3) = {Math.Atan2(+0.3, 1.1):F6}");

        // For special values double.PositiveInfinity
        Console.WriteLine($"atanl(INFINITY) = {Math.Atan2(double.PositiveInfinity, 1.5):F4}, 2 * atanl(INFINITY) = {2 * Math.Atan2(double.PositiveInfinity, 1.5):F5}\n");
    }
}
JavaScript
// Function to calculate the arctangent and demonstrate its use
function calculateArctangentExamples() {
    // For function Math.atan()
    console.log(`Math.atan(1) = ${Math.atan(1)}, 4 * Math.atan(1) = ${4 * Math.atan(1)}`);
    console.log(`Math.atan(-0.0) = ${Math.atan(-0.0)}, Math.atan(+0.0) = ${Math.atan(0)}`);

    // For special values INFINITY
    console.log(`Math.atan(Infinity) = ${Math.atan(Infinity)}, 2 * Math.atan(Infinity) = ${2 * Math.atan(Infinity)}\n`);

    // For function Math.atan2()
    console.log(`Math.atan2(1.1) = ${Math.atan2(1.1)}, 4 * Math.atan2(1.5) = ${4 * Math.atan2(1.5)}`);
    console.log(`Math.atan2(-0.3) = ${Math.atan2(-0.3)}, Math.atan2(+0.3) = ${Math.atan2(0.3)}`);

    // For special values INFINITY
    console.log(`Math.atan2(Infinity) = ${Math.atan2(Infinity)}, 2 * Math.atan2(Infinity) = ${2 * Math.atan2(Infinity)}\n`);

    // For function Math.atan()
    console.log(`Math.atan(1.1) = ${Math.atan(1.1)}, 4 * Math.atan(1.7) = ${4 * Math.atan(1.7)}`);
    console.log(`Math.atan(-1.3) = ${Math.atan(-1.3)}, Math.atan(+0.3) = ${Math.atan(0.3)}`);

    // For special values INFINITY
    console.log(`Math.atan(Infinity) = ${Math.atan(Infinity)}, 2 * Math.atan(Infinity) = ${2 * Math.atan(Infinity)}\n`);
}

// Call the function to calculate arctangent examples
calculateArctangentExamples();

Output
atan(1) = 0.785398,  4*atan(1)=3.141593
atan(-0.0) = -0.000000, atan(+0.0) = +0.000000
atan(Inf) = 1.570796, 2*atan(Inf) = 3.141593

atanf(1.1) = 0.832981, 4*atanf(1.5)=3.931175
atanf(-0.3) = -0.291457, atanf(+0.3) = +0.291457
atanf(Inf) = 1.570796, 2*atanf(Inf) = 3.141593

atanl(1.1) = 0.832981, 4*atanl(1.7)=4.156289
atanl(-1.3) = -0.915101, atanl(+0.3) = +0.291457
atanl(Inf) = 1.570796, 2*atanl(Inf) = 3.141593

Program 2: This program will illustrate the functions catan(), catanf(), and catanl() computes the principal value of the arc tangent of complex number as argument. 

C++
#include <complex>
#include <iostream>
#include <cmath> // Required for mathematical functions

int main() {
    // Given Complex Number
    std::complex<double> z1 = std::atan(std::complex<double>(0, 2));

    // Function atan()
    std::cout << "atan(+0 + 2i) = " << z1.real() << " + " << z1.imag() << "i" << std::endl;

    // Complex(0, + INFINITY)
    std::complex<double> z2 = 2.0 * std::atan(std::complex<double>(0, INFINITY)); // Use 2.0 instead of 2
    std::cout << "2*atan(+0 + i*Inf) = " << z2.real() << " + " << z2.imag() << "i" << std::endl;

    std::cout << std::endl;

    // Function atanf()
    std::complex<float> z3 = std::atan(std::complex<float>(0, 2));
    std::cout << "atanf(+0 + 2i) = " << z3.real() << " + " << z3.imag() << "i" << std::endl;

    // Complex(0, + INFINITY)
    std::complex<float> z4 = 2.0f * std::atan(std::complex<float>(0, INFINITY)); // Use 2.0f instead of 2
    std::cout << "2*atanf(+0 + i*Inf) = " << z4.real() << " + " << z4.imag() << "i" << std::endl;

    std::cout << std::endl;

    // Function atanl()
    std::complex<long double> z5 = std::atan(std::complex<long double>(0, 2));
    std::cout << "atan(+0+2i) = " << z5.real() << " + " << z5.imag() << "i" << std::endl;

    // Complex(0, + INFINITY)
    std::complex<long double> z6 = 2.0L * std::atan(std::complex<long double>(0, INFINITY)); // Use 2.0L instead of 2
    std::cout << "2*atanl(+0 + i*Inf) = " << z6.real() << " + " << z6.imag() << "i" << std::endl;

    return 0;
}
C
// C program to illustrate the use
// of functions catan(), catanf(),
// and catanl()
#include <complex.h>
#include <float.h>
#include <stdio.h>

// Driver Code
int main()
{
    // Given Complex Number
    double complex z1 = catan(2 * I);

    // Function catan()
    printf("catan(+0 + 2i) = %lf + %lfi\n",
           creal(z1), cimag(z1));

    // Complex(0, + INFINITY)
    double complex z2 = 2
                        * catan(2 * I * DBL_MAX);
    printf("2*catan(+0 + i*Inf) = %lf%+lfi\n",
           creal(z2), cimag(z2));

    printf("\n");

    // Function catanf()
    float complex z3 = catanf(2 * I);
    printf("catanf(+0 + 2i) = %f + %fi\n",
           crealf(z3), cimagf(z3));

    // Complex(0, + INFINITY)
    float complex z4 = 2
                       * catanf(2 * I * DBL_MAX);
    printf("2*catanf(+0 + i*Inf) = %f + %fi\n",
           crealf(z4), cimagf(z4));

    printf("\n");

    // Function catanl()
    long double complex z5 = catanl(2 * I);
    printf("catan(+0+2i) = %Lf%+Lfi\n",
           creall(z5), cimagl(z5));

    // Complex(0, + INFINITY)
    long double complex z6 = 2
                             * catanl(2 * I * DBL_MAX);
    printf("2*catanl(+0 + i*Inf) = %Lf + %Lfi\n",
           creall(z6), cimagl(z6));
}
Java
import java.util.*;
import java.lang.Math; // Required for mathematical functions

public class Main {
    public static void main(String[] args) {
        // Given Complex Number
        double realPart = 0;
        double imagPart = 2;
        double angle = Math.atan2(imagPart, realPart);
        double magnitude = Math.sqrt(realPart * realPart + imagPart * imagPart);
        System.out.println("atan(+0 + 2i) = " + angle + " + " + magnitude + "i");

        // Complex(0, + INFINITY)
        double realPart2 = 0;
        double imagPart2 = Double.POSITIVE_INFINITY;
        double angle2 = Math.atan2(imagPart2, realPart2);
        double magnitude2 = Math.sqrt(realPart2 * realPart2 + imagPart2 * imagPart2);
        System.out.println("2*atan(+0 + i*Inf) = " + angle2 + " + " + magnitude2 + "i");

        System.out.println();

        // Function atanf() - Java doesn't have atanf(), it's just atan() for float and double
        float realPart3 = 0;
        float imagPart3 = 2;
        float angle3 = (float)Math.atan2(imagPart3, realPart3);
        float magnitude3 = (float)Math.sqrt(realPart3 * realPart3 + imagPart3 * imagPart3);
        System.out.println("atanf(+0 + 2i) = " + angle3 + " + " + magnitude3 + "i");

        // Complex(0, + INFINITY)
        float realPart4 = 0;
        float imagPart4 = Float.POSITIVE_INFINITY;
        float angle4 = (float)Math.atan2(imagPart4, realPart4);
        float magnitude4 = (float)Math.sqrt(realPart4 * realPart4 + imagPart4 * imagPart4);
        System.out.println("2*atanf(+0 + i*Inf) = " + angle4 + " + " + magnitude4 + "i");

        System.out.println();

        // Function atanl() - Java doesn't have atanl(), it's just atan() for double
        double realPart5 = 0;
        double imagPart5 = 2;
        double angle5 = Math.atan2(imagPart5, realPart5);
        double magnitude5 = Math.sqrt(realPart5 * realPart5 + imagPart5 * imagPart5);
        System.out.println("atan(+0+2i) = " + angle5 + " + " + magnitude5 + "i");

        // Complex(0, + INFINITY)
        double realPart6 = 0;
        double imagPart6 = Double.POSITIVE_INFINITY;
        double angle6 = Math.atan2(imagPart6, realPart6);
        double magnitude6 = Math.sqrt(realPart6 * realPart6 + imagPart6 * imagPart6);
        System.out.println("2*atanl(+0 + i*Inf) = " + angle6 + " + " + magnitude6 + "i");
    }
}
//This code is contributed by utkarsh
Python
import cmath  # Required for complex math functions

# Given Complex Number
z1 = cmath.atan(0 + 2j)

# Function atan()
print("atan(+0 + 2i) =", z1.real, "+", z1.imag, "i")

# Complex(0, + INFINITY)
z2 = 2.0 * cmath.atan(0 + float('inf'))  # Use 2.0 instead of 2
print("2*atan(+0 + i*Inf) =", z2.real, "+", z2.imag, "i")

print()

# Function atanf()
z3 = cmath.atan(0 + 2j)
print("atanf(+0 + 2i) =", z3.real, "+", z3.imag, "i")

# Complex(0, + INFINITY)
z4 = 2.0 * cmath.atan(0 + float('inf'))  # Use 2.0 instead of 2
print("2*atanf(+0 + i*Inf) =", z4.real, "+", z4.imag, "i")

print()

# Function atanl()
z5 = cmath.atan(0 + 2j)
print("atan(+0+2i) =", z5.real, "+", z5.imag, "i")

# Complex(0, + INFINITY)
z6 = 2.0 * cmath.atan(0 + float('inf'))  # Use 2.0 instead of 2
print("2*atanl(+0 + i*Inf) =", z6.real, "+", z6.imag, "i")
#this code is contribbuted by Utkarsh
C#
using System;

class Program
{
    static void Main()
    {
        // Given Complex Number
        double realPart = 0;
        double imagPart = 2;
        double angle = Math.Atan2(imagPart, realPart);
        double magnitude = Math.Sqrt(realPart * realPart + imagPart * imagPart);
        Console.WriteLine("atan(+0 + 2i) = " + angle + " + " + magnitude + "i");

        // Complex(0, + INFINITY)
        double realPart2 = 0;
        double imagPart2 = double.PositiveInfinity;
        double angle2 = Math.Atan2(imagPart2, realPart2);
        double magnitude2 = Math.Sqrt(realPart2 * realPart2 + imagPart2 * imagPart2);
        Console.WriteLine("2*atan(+0 + i*Inf) = " + angle2 + " + " + magnitude2 + "i");

        Console.WriteLine();

        // Function atanf() - C# doesn't have atanf(), it's just atan() for float and double
        float realPart3 = 0;
        float imagPart3 = 2;
        float angle3 = (float)Math.Atan2(imagPart3, realPart3);
        float magnitude3 = (float)Math.Sqrt(realPart3 * realPart3 + imagPart3 * imagPart3);
        Console.WriteLine("atanf(+0 + 2i) = " + angle3 + " + " + magnitude3 + "i");

        // Complex(0, + INFINITY)
        float realPart4 = 0;
        float imagPart4 = float.PositiveInfinity;
        float angle4 = (float)Math.Atan2(imagPart4, realPart4);
        float magnitude4 = (float)Math.Sqrt(realPart4 * realPart4 + imagPart4 * imagPart4);
        Console.WriteLine("2*atanf(+0 + i*Inf) = " + angle4 + " + " + magnitude4 + "i");

        Console.WriteLine();

        // Function atanl() - C# doesn't have atanl(), it's just atan() for double
        double realPart5 = 0;
        double imagPart5 = 2;
        double angle5 = Math.Atan2(imagPart5, realPart5);
        double magnitude5 = Math.Sqrt(realPart5 * realPart5 + imagPart5 * imagPart5);
        Console.WriteLine("atan(+0+2i) = " + angle5 + " + " + magnitude5 + "i");

        // Complex(0, + INFINITY)
        double realPart6 = 0;
        double imagPart6 = double.PositiveInfinity;
        double angle6 = Math.Atan2(imagPart6, realPart6);
        double magnitude6 = Math.Sqrt(realPart6 * realPart6 + imagPart6 * imagPart6);
        Console.WriteLine("2*atanl(+0 + i*Inf) = " + angle6 + " + " + magnitude6 + "i");
    }
}
JavaScript
// Given Complex Number
let realPart = 0;
let imagPart = 2;
let angle = Math.atan2(imagPart, realPart);
let magnitude = Math.sqrt(realPart * realPart + imagPart * imagPart);
console.log("atan(+0 + 2i) = " + angle + " + " + magnitude + "i");

// Complex(0, + INFINITY)
let realPart2 = 0;
let imagPart2 = Infinity;
let angle2 = Math.atan2(imagPart2, realPart2);
let magnitude2 = Math.sqrt(realPart2 * realPart2 + imagPart2 * imagPart2);
console.log("2*atan(+0 + i*Inf) = " + angle2 + " + " + magnitude2 + "i");

console.log();

// Function atanf() - JavaScript doesn't have atanf(), it's just atan() for float and double
let realPart3 = 0;
let imagPart3 = 2;
let angle3 = Math.atan2(imagPart3, realPart3);
let magnitude3 = Math.sqrt(realPart3 * realPart3 + imagPart3 * imagPart3);
console.log("atanf(+0 + 2i) = " + angle3 + " + " + magnitude3 + "i");

// Complex(0, + INFINITY)
let realPart4 = 0;
let imagPart4 = Infinity;
let angle4 = Math.atan2(imagPart4, realPart4);
let magnitude4 = Math.sqrt(realPart4 * realPart4 + imagPart4 * imagPart4);
console.log("2*atanf(+0 + i*Inf) = " + angle4 + " + " + magnitude4 + "i");

console.log();

// Function atanl() - JavaScript doesn't have atanl(), it's just atan() for double
let realPart5 = 0;
let imagPart5 = 2;
let angle5 = Math.atan2(imagPart5, realPart5);
let magnitude5 = Math.sqrt(realPart5 * realPart5 + imagPart5 * imagPart5);
console.log("atan(+0+2i) = " + angle5 + " + " + magnitude5 + "i");

// Complex(0, + INFINITY)
let realPart6 = 0;
let imagPart6 = Infinity;
let angle6 = Math.atan2(imagPart6, realPart6);
let magnitude6 = Math.sqrt(realPart6 * realPart6 + imagPart6 * imagPart6);
console.log("2*atanl(+0 + i*Inf) = " + angle6 + " + " + magnitude6 + "i");

Output
catan(+0 + 2i) = 1.570796 + 0.549306i
2*catan(+0 + i*Inf) = 3.141593+0.000000i

catanf(+0 + 2i) = 1.570796 + 0.549306i
2*catanf(+0 + i*Inf) = 3.141593 + 0.000000i

catan(+0+2i) = 1.570796+0.549306i
2*catanl(+0 + i*Inf) = 3.141593 + 0.000000i

Program 3: This program will illustrate the functions catanh(), catanhf(), and catanhl() computes the complex arc hyperbolic tangent of z along the real axis and in the interval [-i*PI/2, +i*PI/2] along the imaginary axis. 

C++
#include <iostream>
#include <complex>

int main() {
    // Function catanh()
    std::complex<double> z1 = std::atanh(2);
    std::cout << "catanh(+2+0i) = " << z1.real() << std::showpos << z1.imag() << "i" << std::endl;

    // For any z, atanh(z) = atan(iz)/i
    std::complex<double> z2 = std::atanh(std::complex<double>(1, 2));
    std::cout << "catanh(1+2i) = " << z2.real() << std::showpos << z2.imag() << "i" << std::endl << std::endl;

    // Function catanhf()
    std::complex<float> z3 = std::atanh(2.0f);
    std::cout << "catanhf(+2+0i) = " << z3.real() << std::showpos << z3.imag() << "i" << std::endl;

    // For any z, atanh(z) = atan(iz)/i
    std::complex<float> z4 = std::atanh(std::complex<float>(1, 2));
    std::cout << "catanhf(1+2i) = " << z4.real() << std::showpos << z4.imag() << "i" << std::endl << std::endl;

    // Function catanhl()
    std::complex<long double> z5 = std::atanh(2.0L);
    std::cout << "catanhl(+2+0i) = " << z5.real() << std::showpos << z5.imag() << "i" << std::endl;

    // For any z, atanh(z) = atan(iz)/i
    std::complex<long double> z6 = std::atanh(std::complex<long double>(1, 2));
    std::cout << "catanhl(1+2i) = " << z6.real() << std::showpos << z6.imag() << "i" << std::endl << std::endl;

    return 0;
}
C
// C program to illustrate the use
// of functions  catanh(), catanhf(),
// and catanhl()
#include <complex.h>
#include <stdio.h>

// Driver Code
int main()
{
    // Function catanh()
    double complex z1 = catanh(2);
    printf("catanh(+2+0i) = %lf%+lfi\n",
           creal(z1), cimag(z1));

    // for any z, atanh(z) = atan(iz)/i
    // I denotes Imaginary
    // part of the complex number
    double complex z2 = catanh(1 + 2 * I);
    printf("catanh(1+2i) = %lf%+lfi\n\n",
           creal(z2), cimag(z2));

    // Function catanhf()
    float complex z3 = catanhf(2);
    printf("catanhf(+2+0i) = %f%+fi\n",
           crealf(z3), cimagf(z3));

    // for any z, atanh(z) = atan(iz)/i
    float complex z4 = catanhf(1 + 2 * I);
    printf("catanhf(1+2i) = %f%+fi\n\n",
           crealf(z4), cimagf(z4));

    // Function catanh()
    long double complex z5 = catanhl(2);
    printf("catanhl(+2+0i) = %Lf%+Lfi\n",
           creall(z5), cimagl(z5));

    // for any z, atanh(z) = atan(iz)/i
    long double complex z6 = catanhl(1 + 2 * I);
    printf("catanhl(1+2i) = %Lf%+Lfi\n\n",
           creall(z6), cimagl(z6));
}
Python
import cmath

# Function catanh()
z1 = cmath.atanh(2)
print(f"catanh(+2+0i) = {z1.real}{z1.imag:+}i")

# For any z, atanh(z) = atan(iz)/i
z2 = cmath.atanh(1 + 2j)
print(f"catanh(1+2i) = {z2.real}{z2.imag:+}i\n")

# Function catanhf()
z3 = cmath.atanh(2)
print(f"catanhf(+2+0i) = {z3.real}{z3.imag:+}i")

# For any z, atanh(z) = atan(iz)/i
z4 = cmath.atanh(1 + 2j)
print(f"catanhf(1+2i) = {z4.real}{z4.imag:+}i\n")

# Function catanhl()
z5 = cmath.atanh(2)
print(f"catanhl(+2+0i) = {z5.real}{z5.imag:+}i")

# For any z, atanh(z) = atan(iz)/i
z6 = cmath.atanh(1 + 2j)
print(f"catanhl(1+2i) = {z6.real}{z6.imag:+}i\n")
JavaScript
function atan(z) {
    if (z.real === 0) {
        if (z.imag === 1) {
            return Math.PI / 2;
        } else if (z.imag === -1) {
            return -Math.PI / 2;
        } else {
            return NaN;
        }
    }

    let den = z.real * z.real + (1 - z.imag) * (1 - z.imag);
    let realPart = Math.atan2(z.imag, z.real) / den;
    let imagPart = 0.5 * Math.log((z.real * z.real + z.imag * z.imag) / den);

    return { real: realPart, imag: imagPart };
}

// Example usage
let z1 = { real: 2, imag: 0 };
let result1 = atan(z1);
console.log("catanh(+2+0i) =", result1.real, "+", result1.imag, "i");

let z2 = { real: 1, imag: 2 };
let result2 = atan(z2);
console.log("catanh(1+2i) =", result2.real, "+", result2.imag, "i");

Output
catanh(+2+0i) = 0.549306+1.570796i
catanh(1+2i) = 0.173287+1.178097i

catanhf(+2+0i) = 0.549306+1.570796i
catanhf(1+2i) = 0.173287+1.178097i

catanhl(+2+0i) = 0.549306+1.570796i
catanhl(1+2i) = 0.173287+1.178097i
Comment