文章目录
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 float64) float64
返回一个不大于x的最小整数,简单来说就是向下取整
func testFloor() {
fmt.Println(math.Floor(2.9999)) //2
}
2.4 Trunc函数
func Trunc(x float64)