- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Cmplx Package in Golang

Golang is a popular programming language with a wide range of standard libraries that allow programmers to perform complex tasks with ease. The cmplx package is one such library that offers complex number operations in Go. In this article, we will explore the cmplx package, its functions, and how to use them in your programs.

## Overview of the cmplx Package

The cmplx package is part of the Go standard library and provides operations for complex numbers. A complex number is a number that has both real and imaginary components. The cmplx package offers a range of functions for working with complex numbers such as addition, subtraction, multiplication, division, and more.

## Functions Provided by the cmplx Package

The cmplx package offers a range of functions for performing operations on complex numbers. Here are some of the commonly used functions −

**Abs()**− The Abs() function returns the absolute value of a complex number. The absolute value of a complex number is the distance from the origin to the point on the complex plane that represents the number.**Conjugate()**− The Conjugate() function returns the conjugate of a complex number. The conjugate of a complex number is the number obtained by changing the sign of its imaginary component.**Polar()**− The Polar() function returns the polar coordinates of a complex number. The polar coordinates of a complex number are the distance from the origin and the angle between the positive real axis and the line joining the origin to the point representing the number.**Rect()**− The Rect() function returns the rectangular coordinates of a complex number. The rectangular coordinates of a complex number are the real and imaginary components.**Exp()**− The Exp() function returns the exponential of a complex number.**Log()**− The Log() function returns the natural logarithm of a complex number.**Sin()**− The Sin() function returns the sine of a complex number.**Cos()**− The Cos() function returns the cosine of a complex number.**Tan()**− The Tan() function returns the tangent of a complex number.

### Example

Let's look at an example that demonstrates how to use the cmplx package to perform operations on complex numbers.

package main import ( "fmt" "math/cmplx" ) func main() { // Create two complex numbers z1 := complex(2, 3) z2 := complex(4, 5) // Calculate the sum of the two complex numbers sum := z1 + z2 fmt.Println("Sum:", sum) // Calculate the product of the two complex numbers product := z1 * z2 fmt.Println("Product:", product) // Calculate the absolute value of a complex number abs := cmplx.Abs(z1) fmt.Println("Absolute value of z1:", abs) // Calculate the polar coordinates of a complex number r, theta := cmplx.Polar(z1) fmt.Printf("Polar coordinates of z1: r = %f, theta = %f radians\n", r, theta) // Calculate the exponential of a complex number exp := cmplx.Exp(z1) fmt.Println("Exponential of z1:", exp) // Calculate the natural logarithm of a complex number log := cmplx.Log(z1) fmt.Println("Natural logarithm of z1:", log) // Calculate the sine of a complex number sin := cmplx.Sin(z1) fmt.Println("Sine of z1:", sin) // Calculate the cosine of a complex number cos := cmplx.Cos(z1) fmt.Println("Cosine of z1:", cos) // Calculate the tangent of a complex number tan := cmplx.Tan(z1) fmt.Println("Tangent of z1:", tan) }

### Output

Sum: (6+8i) Product: (-7+22i) Absolute value of z1: 3.6055512754639896 Polar coordinates of z1: r = 3.605551, theta = 0.982794 radians Exponential of z1: (-7.315110094901103+1.0427436562359045i) Natural logarithm of z1: (1.2824746787307684+0.982793723247329i) Sine of z1: (9.154499146911428-4.168906959966565i) Cosine of z1: (-4.189625690968807-9.109227893755337i) Tangent of z1: (-0.0037640256415042484+1.0032386273536098i)

### Code Explanation

The cmplx package in Golang provides functions for complex number operations and mathematical functions.

In the code, we have imported the fmt and math/cmplx packages.

Two complex numbers z1 and z2 are created using the complex function.

The sum and product of the two complex numbers are calculated using the + and * operators respectively.

The absolute value of z1 is calculated using the Abs function.

The polar coordinates of z1 are calculated using the Polar function, which returns the magnitude and phase angle in radians.

The exponential of z1 is calculated using the Exp function.

The natural logarithm of z1 is calculated using the Log function.

The sine, cosine, and tangent of z1 are calculated using the Sin, Cos, and Tan functions respectively.

## Conclusion

The cmplx package in Golang provides a range of functions for complex number operations and mathematical functions. These functions can be used to perform complex number calculations and operations in Golang.