Go 语言程序从复数中获取实部


在本文中,我们将讨论如何在 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() 函数获取复数的实部。

  • Go 语言中的 real() 函数是一个预定义函数,它接受一个复数作为参数,并返回该复数的实部(作为浮点数)。

  • 将 real() 函数返回的值存储到一个单独的变量中。在这个特定的示例中,我们使用 real_part 变量来存储实部值。请注意,real_part 是一个 32 位浮点型变量,而不是复数类型变量。

  • 在屏幕上打印结果。

  • 为了打印结果,我们使用了 fmt.Println() 函数。

示例 2

算法

  • 步骤 1 − 导入 fmt 包。

  • 步骤 2 − 初始化并定义 complex_number() 函数,该函数将包含获取复数实部的逻辑。

  • 步骤 3 − 开始 main 函数()

  • 步骤 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 语言程序,该程序将通过示例获取复数的实部。

更新于: 2022年11月15日

486 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.