math包实现的就是数学函数计算。

math

提供了三角函数,幂次函数,特殊函数,类型转化函数等还有其他函数。

三角函数

正弦函数,反正弦函数,双曲正弦,反双曲正弦

  • func Sin(x float64) float64
  • func Asin(x float64) float64
  • func Sinh(x float64) float64
  • func Asinh(x float64) float64

一次性返回 sin,cos

func Sincos(x float64) (sin, cos float64)

余弦函数,反余弦函数,双曲余弦,反双曲余弦

  • func Cos(x float64) float64
  • func Acos(x float64) float64
  • func Cosh(x float64) float64
  • func Acosh(x float64) float64

正切函数,反正切函数,双曲正切,反双曲正切

  • func Tan(x float64) float64
  • func Atan(x float64) float64 和 func Atan2(y, x float64) float64
  • func Tanh(x float64) float64
  • func Atanh(x float64) float64

幂次函数

  • func Cbrt(x float64) float64 // 立方根函数
  • func Pow(x, y float64) float64 // x 的幂函数
  • func Pow10(e int) float64 // 10 根的幂函数
  • func Sqrt(x float64) float64 // 平方根
  • func Log(x float64) float64 // 对数函数
  • func Log10(x float64) float64 // 10 为底的对数函数
  • func Log2(x float64) float64 // 2 为底的对数函数
  • func Log1p(x float64) float64 // log(1 + x)
  • func Logb(x float64) float64 // 相当于 log2(x) 的绝对值
  • func Ilogb(x float64) int // 相当于 log2(x) 的绝对值的整数部分
  • func Exp(x float64) float64 // 指数函数
  • func Exp2(x float64) float64 // 2 为底的指数函数
  • func Expm1(x float64) float64 // Exp(x) - 1

特殊函数

  • func Inf(sign int) float64 // 正无穷
  • func IsInf(f float64, sign int) bool // 是否正无穷
  • func NaN() float64 // 无穷值
  • func IsNaN(f float64) (is bool) // 是否是无穷值
  • func Hypot(p, q float64) float64 // 计算直角三角形的斜边长

类型转化函数

  • func Float32bits(f float32) uint32 // float32 和 unit32 的转换
  • func Float32frombits(b uint32) float32 // uint32 和 float32 的转换
  • func Float64bits(f float64) uint64 // float64 和 uint64 的转换
  • func Float64frombits(b uint64) float64 // uint64 和 float64 的转换

其他函数

  • func Abs(x float64) float64 // 绝对值函数
  • func Ceil(x float64) float64 // 向上取整
  • func Floor(x float64) float64 // 向下取整
  • func Mod(x, y float64) float64 // 取模
  • func Modf(f float64) (int float64, frac float64) // 分解 f,以得到 f 的整数和小数部分
  • func Frexp(f float64) (frac float64, exp int) // 分解 f,得到 f 的位数和指数
  • func Max(x, y float64) float64 // 取大值
  • func Min(x, y float64) float64 // 取小值
  • func Dim(x, y float64) float64 // 复数的维数
  • func J0(x float64) float64 // 0 阶贝塞尔函数
  • func J1(x float64) float64 // 1 阶贝塞尔函数
  • func Jn(n int, x float64) float64 // n 阶贝塞尔函数
  • func Y0(x float64) float64 // 第二类贝塞尔函数 0 阶
  • func Y1(x float64) float64 // 第二类贝塞尔函数 1 阶
  • func Yn(n int, x float64) float64 // 第二类贝塞尔函数 n 阶
  • func Erf(x float64) float64 // 误差函数
  • func Erfc(x float64) float64 // 余补误差函数
  • func Copysign(x, y float64) float64 // 以 y 的符号返回 x 值
  • func Signbit(x float64) bool // 获取 x 的符号
  • func Gamma(x float64) float64 // 伽玛函数
  • func Lgamma(x float64) (lgamma float64, sign int) // 伽玛函数的自然对数
  • func Ldexp(frac float64, exp int) float64 // value 乘以 2 的 exp 次幂
  • func Nextafter(x, y float64) (r float64) // 返回参数 x 在参数 y 方向上可以表示的最接近的数值,若 x 等于 y,则返回 x
  • func Nextafter32(x, y float32) (r float32) // 返回参数 x 在参数 y 方向上可以表示的最接近的数值,若 x 等于 y,则返回 x
  • func Remainder(x, y float64) float64 // 取余运算
  • func Trunc(x float64) float64 // 截取函数

实例

math包中定义的常量

package main

import (
    "fmt"
    "math"
)

func main() {

    fmt.Printf("float64的最大值是:%.f\n", math.MaxFloat64)
    fmt.Printf("float64的最小值是:%.f\n", math.SmallestNonzeroFloat64)
    fmt.Printf("float32的最大值是:%.f\n", math.MaxFloat32)
    fmt.Printf("float32的最小值是:%.f\n", math.SmallestNonzeroFloat32)
    fmt.Printf("Int8的最大值是:%d\n", math.MaxInt8)
    fmt.Printf("Int8的最小值是:%d\n", math.MinInt8)
    fmt.Printf("Uint8的最大值是:%d\n", math.MaxUint8)
    fmt.Printf("Int16的最大值是:%d\n", math.MaxInt16)
    fmt.Printf("Int16的最小值是:%d\n", math.MinInt16)
    fmt.Printf("Uint16的最大值是:%d\n", math.MaxUint16)
    fmt.Printf("Int32的最大值是:%d\n", math.MaxInt32)
    fmt.Printf("Int32的最小值是:%d\n", math.MinInt32)
    fmt.Printf("Uint32的最大值是:%d\n", math.MaxUint32)
    fmt.Printf("Int64的最大值是:%d\n", math.MaxInt64)
    fmt.Printf("Int64的最小值是:%d\n", math.MinInt64)
    //fmt.Println("Uint64的最大值是:", math.MaxUint64)
    fmt.Printf("圆周率默认为:%.200f\n", math.Pi)

}

常用函数

package main

import (
    "fmt"
    "math"
)

func main() {
    /*
        取绝对值,函数签名如下:
            func Abs(x float64) float64
    */
    fmt.Printf("[-3.14]的绝对值为:[%.2f]\n", math.Abs(-3.14))

    /*
        取x的y次方,函数签名如下:
            func Pow(x, y float64) float64
    */
    fmt.Printf("[2]的16次方为:[%.f]\n", math.Pow(2, 16))

    /*
        取余数,函数签名如下:
            func Pow10(n int) float64
    */
    fmt.Printf("10的[3]次方为:[%.f]\n", math.Pow10(3))

    /*
        取x的开平方,函数签名如下:
            func Sqrt(x float64) float64
    */
    fmt.Printf("[64]的开平方为:[%.f]\n", math.Sqrt(64))

    /*
        取x的开立方,函数签名如下:
            func Cbrt(x float64) float64
    */
    fmt.Printf("[27]的开立方为:[%.f]\n", math.Cbrt(27))

    /*
        向上取整,函数签名如下:
            func Ceil(x float64) float64
    */
    fmt.Printf("[3.14]向上取整为:[%.f]\n", math.Ceil(3.14))

    /*
        向下取整,函数签名如下:
            func Floor(x float64) float64
    */
    fmt.Printf("[8.75]向下取整为:[%.f]\n", math.Floor(8.75))

    /*
        取余数,函数签名如下:
            func Floor(x float64) float64
    */
    fmt.Printf("[10/3]的余数为:[%.f]\n", math.Mod(10, 3))

    /*
        分别取整数和小数部分,函数签名如下:
            func Modf(f float64) (int float64, frac float64)
    */
    Integer, Decimal := math.Modf(3.14159265358979)
    fmt.Printf("[3.14159265358979]的整数部分为:[%.f],小数部分为:[%.14f]\n", Integer, Decimal)

}

rand

rand包实现了伪随机数生成器。

import "math/rand"

随机数从资源生成。包水平的函数都使用的默认的公共资源。该资源会在程序每次运行时都产生确定的序列。如果需要每次运行产生不同的序列,应使用Seed函数进行初始化。如果不调用sned函数,默认都是采用sned(1),默认资源可以安全的用于多go程并发。

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    for i := 0; i < 10; i++ {
        x := rand.Intn(100)
        fmt.Println(x)
    }
}

以上例子是打印10个100以内(0-99)的随机数字

  • Rand.Intn(n)。int代表整数,后面的n代表范围,其他类型类似
  • rand.Seed 设置随机数种子
  • time.Now().UnixNano() 种子,也就是随机因子,相同的种子产生的随机数是一样的。

自定义生成Rand结构体,设置随机数种子

func NewSource(seed int64) Source

使用给定的种子创建一个伪随机资源。

func New(src Source) *Rand

返回一个使用src生产的随机数来生成其他各种分布的随机数值的*Rand。

实例

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    source := rand.NewSource(time.Now().UnixNano()) // 使用当前的纳秒生成一个随机源,也就是随机种子
    ran := rand.New(source) // 生成一个rand
    fmt.Println(rand.Int())
    fmt.Println(rand.Int31())
    fmt.Println(rand.Intn(5))
}

其它生成随机数的方法

Rand生成随机数当然不只这三个方法,还有其它生成随机数的方法

func (r *Rand) Int63() int64

返回一个int64类型的非负的63位伪随机数。

func (r *Rand) Uint32() uint32

返回一个uint32类型的非负的32位伪随机数。

func Int31n(n int32) int32

返回一个取值范围在[0,n)的伪随机int32值,如果n<=0会panic。

func Int63n(n int64) int64

返回一个取值范围在[0, n)的伪随机int64值,如果n<=0会panic。

func (r *Rand) Float32() float32

返回一个取值范围在[0.0, 1.0)的伪随机float32值。

func (r *Rand) Float64() float64

返回一个取值范围在[0.0, 1.0)的伪随机float64值。

func (r *Rand) Perm(n int) []int

返回一个有n个元素的,[0,n)范围内整数的伪随机排列的切片。

crypto/rand

这个rand包实现了用于加解密的更安全的随机数生成器,主要应用场景:生成随机加密串。

这边简单使用一个实例说明,具体可以看crypto

package main

import (
    "crypto/rand"
    "encoding/base64"
    "fmt"
    "math/big"
)

func main() {
    //1、Int
    n, err := rand.Int(rand.Reader, big.NewInt(128))
    if err == nil {
        fmt.Println("rand.Int:", n, n.BitLen())
    }
    //2、Prime
    p, err := rand.Prime(rand.Reader, 5)
    if err == nil {
        fmt.Println("rand.Prime:", p)
    }
    //3、Read
    b := make([]byte, 32)
    m, err := rand.Read(b)
    if err == nil {
        fmt.Println("rand.Read:", b[:m])
        fmt.Println("rand.Read:", base64.URLEncoding.EncodeToString(b))
    }
}

运行结果

rand.Int: 92 7
rand.Prime: 29
rand.Read: [207 47 241 208 190 84 109 134 86 106 87 223 111 113 203 155 44 118 71 20 186 62 66 130 244 98 97 184 8 179 6 230]
rand.Read: zy_x0L5UbYZWalffb3HLmyx2RxS6PkKC9GJhuAizBuY=

big

大数处理,可以用golang的math/big包

package main

import (
    "fmt"
    "math/big"
)

func main() {
    //设置一个大于int64的数
    a := new(big.Int)
    a, ok := a.SetString("9122322238215458478512545454878168716584545412154785452142499999", 10)
    if !ok {
        panic("error")
    }
    //String方法可以转换成字符串输出
    fmt.Println(a.String())

    //大数相加
    b:=big.NewInt(2)
    b=b.Add(a,b) //  Mod 取模、Add 加、Sub 减、Mul 乘、Div 除
    fmt.Println(b.String())
}

cmplx

cmplx 包为复数提供基本的常量和数学函数。

bits

bits主要打包字节为预先声明的无符号整数类型实现位计数和操作函数。