El lenguaje Go brinda soporte incorporado para constantes básicas y funciones matemáticas para números complejos con la ayuda del paquete cmplx. Puede encontrar la raíz cuadrada del número complejo especificado con la ayuda de la función Sqrt() proporcionada por el paquete math/cmplx. En esta función, q se elige de modo que real(q) >= 0 e imag(q) tenga el mismo signo que imag(y). Por lo tanto, para acceder a la función Sqrt(), debe agregar un paquete matemático/cmplx en su programa con la ayuda de la palabra clave de importación.
Sintaxis:
func Sqrt(y complex128) complex128
Discutamos este concepto con la ayuda de los ejemplos dados:
Ejemplo 1:
// Golang program to illustrate how to find // the square root of the given complex number package main import ( "fmt" "math/cmplx" ) // Main function func main() { // Finding the square root of // the specified complex number // Using Sqrt() function res_1 := cmplx.Sqrt(8 - 6i) res_2 := cmplx.Sqrt(-4 + 12i) res_3 := cmplx.Sqrt(-3 - 9i) // Displaying the result fmt.Printf("Result 1: %.2f", res_1) fmt.Printf("\nResult 2: %.2f", res_2) fmt.Printf("\nResult 3: %.2f", res_3) }
Producción:
Result 1: (3.00-1.00i) Result 2: (2.08+2.89i) Result 3: (1.80-2.50i)
Ejemplo 2:
// Golang program to illustrate how to find // the square root of the given complex number package main import ( "fmt" "math/cmplx" ) // Main function func main() { cnumber_1 := complex(0, 2) cnumber_2 := complex(4, 6) // Finding square root cvalue_1 := cmplx.Sqrt(cnumber_1) cvalue_2 := cmplx.Sqrt(cnumber_2) // Sum of the given square roots res := cvalue_1 + cvalue_2 // Displaying results fmt.Println("Complex Number 1: ", cnumber_1) fmt.Printf("Square Root 1: %.1f", cvalue_1) fmt.Println("\nComplex Number 2: ", cnumber_2) fmt.Printf("Square Root: %.1f ", cvalue_2) fmt.Printf("\nSum : %.1f", res) }
Producción:
Complex Number 1: (0+2i) Square Root 1: (1.0+1.0i) Complex Number 2: (4+6i) Square Root: (2.4+1.3i) Sum : (3.4+2.3i)
Publicación traducida automáticamente
Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA