Golang 从复数中获取实部
在本文中,我们将讨论如何在Go语言中从复数中获取实部。
在数学中,复数是可以以a + ib的形式表示的数字,其中i称为虚数单位。虚数单位i的值为\mathrm{\sqrt{-1}},a和b为实数。
复数的第一部分a被称为复数的实部,虚数单位i后面的部分b被称为复数的虚部。
语法
func real(number complex128) float64
real()函数用于获取任何复数的实部。该函数接受一个64位或128位的复数作为参数。如果复数由两个32位数组成,则其大小为64位;如果由两个64位数组成,则大小为128位。该函数以64位浮点数的形式返回实部。
以下是源代码的编译和执行结果。
示例1
步骤
- 步骤1 - 导入fmt包。
-
步骤2 - 开始main 函数() 。
-
步骤3 - 创建和初始化复数变量。
-
步骤4 - 使用 real() 函数获取所选复数的实部。
-
步骤5 - 在屏幕上打印实部。
示例
package main
import "fmt"
// fmt package allows us to print anything on the screen.
// calling the main() function
func main() {
// initializing the number variable to store the complex number.
var number complex64
// initializing the real_part variable to store real part of complex number.
var real_part float32
// assigning the complex number.
number = 9.89 + 10i
// getting the real part of the complex number.
real_part = real(number)
// printing the result.
fmt.Println( "The real part of the complex number", number, "is",real_part)
}
输出
The real part of the complex number (9.89+10i) is 9.89
代码描述
-
首先,我们导入fmt包,它允许我们打印任何内容。
-
开始 main() 函数。
-
初始化一个复数变量并将一个复数存储在其中。
-
使用real()函数获取复数的实部。
-
real()函数是golang中的预定义函数,它接受一个复数作为参数,并将该复数的实部作为浮点数返回。
-
将 real() 返回的值存储到一个单独的变量中。在这个特定的示例中,我们使用real_part变量来存储实部的值。注意,real_part是一个32位浮点类型变量,不是复数类型变量。
-
在屏幕上打印结果。
-
为了打印结果,我们使用了 fmt.Println() 函数。
示例2
步骤
-
步骤1 − 导入fmt包。
-
步骤2 − 初始化并定义 complex_number() 函数,该函数将包含获取复数实部的逻辑。
-
步骤3 − 启动主要的 函数() 。
-
步骤4 − 初始化并为32位浮点类型变量赋值。
-
步骤5 − 调用 complex_number() 函数。
-
步骤6 − 使用 complex() 函数将上述浮点数值生成复数。
-
步骤7 − 使用 real() 函数获取所选复数的实部。
-
步骤8 − 在屏幕上打印结果。
示例
package main
import "fmt"
// fmt package allows us to print anything on the screen.
// creating the complex function
func complex_number (number1, number2 float32) {
// combining the real and imaginary parts to create the complex number.
// complex() is an inbuilt function in GO language that takes two numbers as inputs and combines //them to produces the complex number.
var complex_num = complex(number1, number2)
// printing the resultant on the screen
fmt.Println("The resultant complex number is : ", complex_num)
// getting the real part of the complex number.
var real_part float32
real_part = real(complex_num)
// printing the real part on the screen.
fmt.Println( "The real part of the entered complex number is :" ,real_part)
}
// calling the main() function
func main() {
// defining the variables that will store the values entered by the user
var number1 float32
var number2 float32
// assigning values to the above defined variables
number1 = 53.987
number2 = -2.85
// calling the above made complex_number() function
complex_number(number1, number2)
}
结果
The resultant complex number is : (53.987-2.85i)
The real part of the entered complex number is : 53.987
代码描述
-
首先,我们导入了 fmt 包,该包允许我们打印任何内容。
-
然后我们创建并定义了 complex_number() 函数,用于获取并打印复数的实部。
-
开始 main() 函数。
-
初始化并赋值给 number1 和 number2 变量。
-
调用 complex_number() 函数,并将上述两个浮点数值作为参数传递给它。
-
使用 complex() 函数从上述两个值创建复数。该函数接受两个参数,将第一个参数视为复数的实部,第二个参数视为虚部。
-
将其返回值存储在一个单独的变量中。现在使用 real() 函数获取上述生成的复数的实部,并将结果存储在一个变量中。在这个特定的示例中,我们使用 real_part 变量来存储实部的值。注意,real_part 是一个 32 位浮点型变量,不是复数类型变量。
-
在屏幕上打印结果。
-
为了打印结果,我们使用了 fmt.Println() 函数。
结论
我们成功编译并执行了一个 Go 语言程序,该程序将获取复数的实部,并提供了示例。