golang学习笔记系列之标准库math的学习

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

同步更新于个人博客系统:golang学习笔记系列之标准库math的学习

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值