Go语言学习笔记—golang标准库math包

文章目录

  • 一 常量
  • 二 常用函数
    • 2.1 IsNaN函数
    • 2.2 Ceil函数
    • 2.3 Floor函数
    • 2.4 Trunc函数
    • 2.5 Abs函数
    • 2.6 Max函数
    • 2.7 Min函数
    • 2.8 Dim函数
    • 2.9 Mod函数
    • 2.10 Sqrt函数
    • 2.11 Cbrt函数
    • 2.12 Hypot函数
    • 2.13 Pow函数
    • 2.14 Sin函数
    • 2.15 Cos函数
    • 2.16 Tan函数
    • 2.17 Log函数
    • 2.18 Log2函数
    • 2.19 Log10函数
    • 2.20 Signbit函数
  • 三 随机数math/rand


math包包含一些常量和一些有用的数学计算函数,例如:三角函数、随机数、绝对值、平方等

一 常量

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.Printf("圆周率默认值: %v\n", math.Pi)

常量如下:

Float64的最大值: 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
Float64最小值: 0
Float32最大值: 340282346638528859811704183484516925440
Float32最小值: 0
Int8最大值: 127
Int8最小值: -128
Uint8最大值: 255
Int16最大值: 32767
Int16最小值: -32768
Uint16最大值: 65535
Int32最大值: 2147483647
Int32最小值: -2147483648
Uint32最大值: 4294967295
Int64最大值: 9223372036854775807
Int64最小值: -9223372036854775808
圆周率默认值: 3.141592653589793

二 常用函数

2.1 IsNaN函数

func IsNaN(f float64) (is bool)

报告f是否表示一个NaN(Not A Number)值,是数值返回一个false,不是数值则返回一个true。

func testIsNaN() {
   
	fmt.Println(math.IsNaN(12321.321321))    //false
}

2.2 Ceil函数

func Ceil(x float64) float64

返回一个不小于x的最小整数,简单来说就是向上取整

func testCeil() {
   
	fmt.Println(math.Ceil(1.13456))    //2
}

2.3 Floor函数

func Floor(x 

你可能感兴趣的:(Go语言进阶学习笔记,golang)