1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

package main

import (
"fmt"
"math"
)

/*
1、func IsNaN(f float64) (is bool)
● 报告f是否表示一个NaN(Not A Number)值。

2、func Ceil(x float64) float64
● 返回不小于x的最小整数(的浮点值)

3、func Floor(x float64) float64
● 返回不大于x的最小整数(的浮点值)

4、func Trunc(x float64) float64
● 返回x的整数部分(的浮点值)。

5、func Abs(x float64) float64
● 返回x的绝对值

6、func Max(x, y float64) float64
● 返回x和y中最大值

7、func Min(x, y float64) float64
● 返回x和y中最小值

8、func Dim(x, y float64) float64
● 函数返回x-y和0中的最大值

9、func Mod(x, y float64) float64
● 取余运算,可以理解为 x-Trunc(x/y)*y,结果的正负号和x相同

10、func Sqrt(x float64) float64
● 返回x的二次方根

11、func Cbrt(x float64) float64
● 返回x的三次方根,特例如下:

12、func Hypot(p, q float64) float64
● 返回Sqrt(p*p + q*q)

13、func Pow(x, y float64) float64
● 返回x^y

14、func Sin(x float64) float64
● 求正弦。

15、func Cos(x float64) float64
● 求余弦。

16、func Tan(x float64) float64
● 求正切。

17、func Log(x float64) float64
● 求自然对数

18、func Log2(x float64) float64
● 求2为底的对数。

19、func Log10(x float64) float64
求10为底的对数。
*/

func main() {
fmt.Println(math.IsNaN(3.4)) //false
fmt.Println(math.Ceil(1.000001)) //2
fmt.Println(math.Floor(1.999999)) //1
fmt.Println(math.Trunc(1.999999)) //1
fmt.Println(math.Abs(-1.3)) //1.3
fmt.Println(math.Max(-1.3, 0)) //0
fmt.Println(math.Min(-1.3, 0)) //-1.3
fmt.Println(math.Dim(-12, -19)) //7
fmt.Println(math.Dim(-12, 19)) //0
fmt.Println(math.Mod(9, 4)) //1
fmt.Println(math.Sqrt(9)) //3
fmt.Println(math.Cbrt(8)) //2
fmt.Println(math.Hypot(3, 4)) //5
fmt.Println(math.Pow(2, 8)) //256
fmt.Println(math.Log(1)) //0
fmt.Println(math.Log2(16)) //4
fmt.Println(math.Log10(1000)) //3

}

本文链接:https://dreamerjonson.com/2018/12/01/golang-29-math-package/

版权声明:本博客所有文章除特别声明外,均采用CC BY 4.0 CN协议许可协议。转载请注明出处!