go语言复数包_Go语言复数类型-Go语言复complex类型-嗨客网

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 语言复数类型的虚部。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值