math包提供了基本的数学常数和数学函数。
常数如下:
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
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)
取值极限:
MaxInt8 = 1<<7 - 1
MinInt8 = -1 << 7
MaxInt16 = 1<<15 - 1
MinInt16 = -1 << 15
MaxInt32 = 1<<31 - 1
MinInt32 = -1 << 31
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint8 = 1<<8 - 1
MaxUint16 = 1<<16 - 1
MaxUint32 = 1<<32 - 1
MaxUint64 = 1<<64 - 1
常用函数如下:
IsNaN函数
func IsNaN(f float64) (is bool)
报告f是否表示一个NaN(Not A Number)值,是数值返回一个false,不是数值则返回一个true。
func testIsNaN() {
fmt.Println(math.IsNaN(12321.321321)) //false
}
Ceil函数
func Ceil(x float64) float64
返回一个不小于x的最小整数,简单来说就是向上取整
func testCeil() {
fmt.Println(math.Ceil(1.13456)) //2
}
Floor函数
func Floor(x float64) float64
返回一个不大于x的最小整数,简单来说就是向下取整
func testFloor() {
fmt.Println(math.Floor(2.9999)) //2
}
Trunc函数
func Trunc(x float64) float64
返回x整数部分,与Floor一样
func testTrunc() {
fmt.Println(math.Trunc(2.9999)) //2
}
Abs函数
func Abs(x float64) float64
返回x的绝对值
func testAbs() {
fmt.Println(math.Abs(2.999312323132141665374)) //2.9993123231321417
fmt.Println(math.Abs(2.999312323132141465374)) //2.9993123231321412
}
Max函数
func Max(x, y float64) float64
返回x和y中最大值
func testMax() {
fmt.Println(math.Max(1000,200)) //1000
}
Min函数
func Min(x, y float64) float64
返回x和y中最小值
func testMin() {
fmt.Println(math.Min(1000,200)) //200
}
Dim函数
func Dim(x, y float64) float64
函数返回x-y和0中的最大值
func testDim() {
fmt.Println(math.Dim(1000,2000)) //0
fmt.Println(math.Dim(1000,200)) //800
}
Mod函数
func Mod(x, y float64) float64
取余运算,可以理解为 x-Trunc(x/y)*y,结果的正负号和x相同
func testMod() {
fmt.Println(math.Mod(123,0)) //NaN
fmt.Println(math.Mod(123,10)) //3
}
Sqrt函数
func Sqrt(x float64) float64
返回x的二次方根,平方根
func testSqrt() {
fmt.Println(math.Sqrt(144)) //12
}
Cbrt函数
func Cbrt(x float64) float64
返回x的二次方根,平方根
func testCbrt() {
fmt.Println(math.Cbrt(1728)) //12
}
Hypot函数
func Hypot(p, q float64) float64
返回Sqrt(p*p q*q),注意要避免不必要的溢出或下溢。
func testHypot() {
fmt.Println(math.Hypot(12,12)) //16.970562748477143
}
Pow函数
func Pow(x, y float64) float64
求幂,x的y次方
func testPow() {
fmt.Println(math.Pow(2,3)) //8
}
Sin函数
func Sin(x float64) float64
求正弦
func testSin() {
fmt.Println(math.Sin(12)) //-0.5365729180004349
}
Cos函数
func Cos(x float64) float64
求余弦
func testCos() {
fmt.Println(math.Cos(12)) //0.8438539587324921
}
Tan函数
func Tan(x float64) float64
求正切
func testTan() {
fmt.Println(math.Tan(12)) //-0.6358599286615807
}
Log函数
func Log(x float64) float64
求自然对数
func testLog() {
fmt.Println(math.Log(2)) //0.6931471805599453
}
Log2函数
func Log2(x float64) float64
求2为底的对数
func testLog2() {
fmt.Println(math.Log2(128)) //7
}
Log10函数
func Log10(x float64) float64
求10为底的对数
func testLog10() {
fmt.Println(math.Log10(10000)) //4
}
Signbit函数
func Signbit(x float64) bool
如果x是一个负数或者负零,返回真
func testSignbit() {
fmt.Println(math.Signbit(10000)) //false
fmt.Println(math.Signbit(-200)) //true
}
来源: