math
math包包含了一些基本的常量和数学函数。
package main
import (
"fmt"
"math"
"math/rand"
"time"
)
func main() {
//常量
fmt.Printf("math.MaxFloat64: %v\n", math.MaxFloat64)
fmt.Printf("math.MaxFloat32: %v\n", math.MaxFloat32)
fmt.Printf("math.MaxInt64: %v\n", math.MaxInt64)
fmt.Printf("math.MaxInt32: %v\n", math.MaxInt32)
fmt.Printf("math.MaxInt16: %v\n", math.MaxInt16)
fmt.Printf("math.MaxInt8: %v\n", math.MaxInt8)
fmt.Printf("math.MaxInt: %v\n", math.MaxInt)
fmt.Printf("math.Pi: %.20f\n", math.Pi)
//数学函数
//绝对值
fmt.Printf("math.Abs(-100): %v\n", math.Abs(-100))
//幂函数
fmt.Printf("math.Pow(2, 3): %v\n", math.Pow(2, 3)) //2^3
//开平方
fmt.Printf("math.Sqrt(4): %v\n", math.Sqrt(4))
//开立方
fmt.Printf("math.Cbrt(8): %v\n", math.Cbrt(8))
//向上取整
fmt.Printf("math.Ceil(10.2): %v\n", math.Ceil(10.2))
//向下取整
fmt.Printf("math.Floor(10.8): %v\n", math.Floor(10.8))
//四舍五入
fmt.Printf("math.Round(10.4): %v\n", math.Round(10.4))
//取余
fmt.Printf("math.Mod(10, 3): %v\n", math.Mod(10, 3))
//三角函数
angle := math.Pi / 180 //1度
fmt.Printf("math.Sin(90): %v\n", math.Sin(90*angle))
fmt.Printf("math.Cos(60): %v\n", math.Cos(60*angle))
fmt.Printf("math.Tan(90): %v\n", math.Tan(45*angle))
//随机数
rand.Seed(time.Now().UnixMicro()) //设置随机数种子
for i := 0; i < 5; i++ {
fmt.Printf("rand.Intn(100): %v\n", rand.Intn(100)) //100以内的随机整数
fmt.Printf("rand.Float64(): %v\n", rand.Float64()) //0到1的随机小数
}
}
运行结果
math.MaxFloat64: 1.7976931348623157e+308
math.MaxFloat32: 3.4028234663852886e+38
math.MaxInt64: 9223372036854775807
math.MaxInt32: 2147483647
math.MaxInt16: 32767
math.MaxInt8: 127
math.MaxInt: 9223372036854775807
math.Pi: 3.14159265358979311600
math.Abs(-100): 100
math.Pow(2, 3): 8
math.Sqrt(4): 2
math.Cbrt(8): 2
math.Ceil(10.2): 11
math.Floor(10.8): 10
math.Round(10.4): 10
math.Mod(10, 3): 1
math.Sin(90): 1
math.Cos(60): 0.5000000000000001
math.Tan(90): 1
rand.Intn(100): 12
rand.Float64(): 0.9683545409741706
rand.Intn(100): 36
rand.Float64(): 0.8207458891921628
rand.Intn(100): 52
rand.Float64(): 0.5214102032604347
rand.Intn(100): 6
rand.Float64(): 0.537895866071663
rand.Intn(100): 99
rand.Float64(): 0.7215569611912539