入门:基础语法(二)内建变量类型

栏目: Go · 发布时间: 6年前

内容简介:golang作为一门通用编程语言,把complex作为内建类型是很少见的,我们这里用golang验证以下欧拉公式:e^(i*PI)+1=0复数所使用的包是cmplx,下面请看代码:

golang内建类型包括:

bool,string,int,uintptr,byte,rune,float,complex

golang作为一门通用编程语言,把complex作为内建类型是很少见的,我们这里用golang验证以下欧拉公式:

e^(i*PI)+1=0

复数所使用的包是cmplx,下面请看代码:

func euler() {
	fmt.Println(cmplx.Pow(math.E, 1i*math.Pi) + 1)
	fmt.Println()
	fmt.Println(cmplx.Exp(1i*math.Pi) + 1)
}

本人的计算机输出的结果是:

(0+1.2246467991473515e-16i)

为什么不是0?很简单,float无法表示0!如果一定要,使用fmt.printf("%.3f")来进行输出的

golang中类型转换是强制的

math.Sqrt(a float)所需要的参数是一个float类型,我们这里求证以下勾股定理

func triangle() {
	var a, b int = 3, 4
	var c int
	c = int(math.Sqrt(float64(a*a + b*b)))
	fmt.Println(c)
}

其他语言有默认的类型转换,但是golang只有强制类型转换!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Nature of Code

The Nature of Code

Daniel Shiffman / The Nature of Code / 2012-12-13 / GBP 19.95

How can we capture the unpredictable evolutionary and emergent properties of nature in software? How can understanding the mathematical principles behind our physical world help us to create digital w......一起来看看 《The Nature of Code》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码