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 el valor absoluto del número complejo especificado con la ayuda de la función Abs() proporcionada por el paquete math/cmplx . Por lo tanto, debe agregar un paquete matemático/cmplx en su programa con la ayuda de la palabra clave de importación para acceder a la función Abs().
Sintaxis:
func Abs(a complex128) float64
Discutamos este concepto con la ayuda de los ejemplos dados:
Ejemplo 1:
// Golang program to illustrate // how to find absolute value package main import ( "fmt" "math/cmplx" ) // Main function func main() { // Finding absolute value of // the specified complex number // Using Abs() function res_1 := cmplx.Abs(3 + 5i) res_2 := cmplx.Abs(-4 + 8i) res_3 := cmplx.Abs(-8 - 7i) // Displaying the result fmt.Println("Random Number 1:", res_1) fmt.Println("Random Number 2: ", res_2) fmt.Println("Random Number 3: ", res_3) }
Producción:
Random Number 1: 5.8309518948453 Random Number 2: 8.94427190999916 Random Number 3: 10.63014581273465
Ejemplo 2:
// Golang program to illustrate how // to find absolute value package main import ( "fmt" "math/cmplx" ) // Main function func main() { // Complex numbers cnumber_1 := complex(5, 7) cnumber_2 := complex(6, 9) // Finding absolute values absvalue_1 := cmplx.Abs(cnumber_1) absvalue_2 := cmplx.Abs(cnumber_2) // Sum of two absolute values res := absvalue_1 + absvalue_2 // Displaying results fmt.Println("Complex Number 1: ", cnumber_1) fmt.Println("Complex Number 2: ", cnumber_2) fmt.Println("Sum of the absolute values of "+ "the given complex numbers: ") fmt.Printf("%.1f + %.1f = %.1f", absvalue_1, absvalue_2, res) }
Producción:
Complex Number 1: (5+7i) Complex Number 2: (6+9i) Sum of the absolute values of the given complex numbers: 8.6 + 10.8 = 19.4
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