Go语言复数类型教程
其中,complex64 的实数部分和虚数部分都是
Go语言复数类型定义详解
语法
complex(real,image)
参数
参数
描述
complex
定义复数类型使用的关键字。
real
复数的实部。
image
复数的虚部。
说明
Go 语言复数类型的定义使用 complex 关键字。real 关键字用于获取 Go 语言复数类型的实部,imag 关键字用于获取 Go 语言复数类型的虚部。
案例
复数类型
使用 complex 关键字,定义一个复数
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello 嗨客网(www.haicoder.net)")
var score complex64 = complex(1,2)
var number complex128 = complex(23.23, 11.11)
fmt.Print("Score = ", score, " Number = ", number)
}
程序运行后,控制台输出如下:
我们使用 complex 定义了一个 complex64 类型的变量 score。我们再次使用 complex 定义了一个 complex128 类型的变量 number。
复数类型
获取复数类型的实部和虚部
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello 嗨客网(www.haicoder.net)")
var score complex64 = complex(1,2)
var number complex128 = complex(23.23, 11.11)
fmt.Print("Real Score = ", real(score), " Image Score = ", imag(score), "\n")
fmt.Print("Real Number = ", real(number), " Image Number = ", imag(number))
}
程序运行后,控制台输出如下:
我们使用 complex 定义了一个 complex64 类型的变量 score。我们再次使用 complex 定义了一个 complex128 类型的变量 number。
使用 real 获取复数的实部,使用 imag 获取复数的虚部。
Go语言复数类型总结
Go 语言提供了两种复数类型,complex64 和 complex128 。real 关键字用于获取 Go 语言复数类型的实部,imag 关键字用于获取 Go 语言复数类型的虚部。