Golang程序 从复数中获取实部

Golang程序 从复数中获取实部

在这篇文章中,我们将讨论如何用Go语言从一个复数中获取实部。

数学中的复数是一个可以用a+ib的形式表示的数字,其中i被称为iota。iota的值是mathrm{\sqrt{-1}},a和b是实数。

第一部分,即’a’被称为复数的实部,iota之后的部分,即’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()函数获取复数的实数部分。

  • golang中的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语言程序,该程序将通过实例获得复数的实部。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程