go语言中文网
godoc
go语言中文网有很多文档缺少内容比如string.Builder就没有,godoc绝对详尽,推荐阅读godoc
math包
常量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
const (
E = 2.71828182845904523536028747135266249775724709369995957496696763 // A001113
Pi = 3.14159265358979323846264338327950288419716939937510582097494459 // A000796
Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // A001622
Sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974 // A002193
SqrtE = 1.64872127070012814684865078781416357165377610071014801157507931 // A019774
SqrtPi = 1.77245385090551602729816748334114518279754945612238712821380779 // A002161
SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // A139339
Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009 // A002162
Log2E = 1 / Ln2
Ln10 = 2.30258509299404568401799145468436420760110148862877297603332790 // A002392
Log10E = 1 / Ln10
)
const (
MaxFloat32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
MaxFloat64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
const (
intSize = 32 << (^uint(0) >> 63) // 32 or 64
MaxInt = 1<<(intSize-1) - 1 // MaxInt32 or MaxInt64 depending on intSize.
MinInt = -1 << (intSize - 1) // MinInt32 or MinInt64 depending on intSize.
MaxInt8 = 1<<7 - 1 // 127
MinInt8 = -1 << 7 // -128
MaxInt16 = 1<<15 - 1 // 32767
MinInt16 = -1 << 15 // -32768
MaxInt32 = 1<<31 - 1 // 2147483647
MinInt32 = -1 << 31 // -2147483648
MaxInt64 = 1<<63 - 1 // 9223372036854775807
MinInt64 = -1 << 63 // -9223372036854775808
MaxUint = 1<<intSize - 1 // MaxUint32 or MaxUint64 depending on intSize.
MaxUint8 = 1<<8 - 1 // 255
MaxUint16 = 1<<16 - 1 // 65535
MaxUint32 = 1<<32 - 1 // 4294967295
MaxUint64 = 1<<64 - 1 // 18446744073709551615
)
|
函数
-
func Ceil(x float64) float64:返回不小于x的最小整数(的浮点值)
-
func Floor(x float64) float64:返回不大于x的最大整数(的浮点值)
-
func Trunc(x float64) float64:返回x的整数部分(的浮点值)
-
func Modf(f float64) (int float64, frac float64):返回f的整数部分和小数部分,结果的正负号都和f相同
-
func Abs(x float64) float64:返回x的绝对值
-
func Round(x float64) float64:四舍五入
-
func Max(x, y float64) float64:返回x和y中最大值
-
func Min(x, y float64) float64:返回x和y中最小值
-
func Sqrt(x float64) float64:返回x的二次方根
-
func Sin(x float64) float64
-
func Cos(x float64) float64
-
func Tan(x float64) float64
-
func Log2 :求2为底的对数
-
func Pow(x, y float64) float64:返回x**y
math/rand
函数
- func Intn(n int) int:返回一个取值范围在[0,n)的伪随机int值,如果n<=0会panic。
type Source interface
Source代表一个生成均匀分布在范围[0, 1«63)的int64值的(伪随机的)资源
方法
- Int63() int64
- Seed(seed int64)
函数
- func NewSource(seed int64) Source:根据给定种子创建伪随机源
type Rand struct
函数
- func New(src Source) *Rand:根据src新建一个rand对象
- func Seed(seed int64):默认rand对象设置种子
- func Int() int
- func Int31() int32
- func Int63() int64
- func Uint32() uint32
- func Intn(n int) int
方法
- func (r *Rand) Seed(seed int64):重新设置种子
- func (r *Rand) Int() int
- func (r *Rand) Int31() int32
- func (r *Rand) Int63() int64
- func (r *Rand) Uint32() uint32
- func (r *Rand) Intn(n int) int
根据时间生成随机数
1
2
|
rand.Seed(time.Now().Unix())
a:=rand.Intn(max)
|
math/big
该包实现任意精度算术,主要是Float,Int和Rat(复数)
type Int struct
重要函数
- func NewInt(x int64 ) * Int:NewInt 分配并返回一个设置为 x 的新 Int
重要方法
- func (x * Int ) Cmp(y * Int ) (r int ):比较x和y的值,
x<y
返回-1,x==y
返回0,x>y
返回1,
- func (x * Int ) Int64() int64:返回x的int64表示,无法表示时结果不确定
- func (z * Int ) Add(x, y * Int ) * Int:x+y的值给z并返回z
- func (z * Int ) Mul(x, y * Int ) * Int:x*y的值给z并返回z
- func (z * Int ) Mod(x, y * Int ) * Int:x%y的值给z并返回z