-
Notifications
You must be signed in to change notification settings - Fork 1
/
math.go
196 lines (159 loc) · 3.87 KB
/
math.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package vfp
import "math"
import "math/rand"
import "strconv"
import "fmt"
func Abs(zfloat float64) float64 {
return math.Abs(zfloat)
}
func Int(zfloat float64) int {
return int(zfloat)
}
/*
Returns a random number between 0 and zrange.
*/
/*Example:
Rand(1 ,100) //1~100
*/
func Rand(zx int, zrange int) int {
var r *rand.Rand
r = rand.New(rand.NewSource(int64(zx)))
return int(r.Intn(zrange))
}
//Returns the sine of an angle.
func Sin(zf float64) float64 {
return math.Sin(zf)
}
//Converts degrees to radians.
func Dtor(zf float64) float64 {
return math.Pi * zf / 180
}
//Converts radians to its equivalent in degrees.
func Rtod(zf float64) float64 {
return 180 * zf / math.Pi
}
//Returns the cosine of a numeric expression.
func Cos(zf float64) float64 {
return math.Cos(zf)
}
//Returns the arc cosine of a specified numeric expression.
func Acos(zf float64) float64 {
return math.Acos(zf)
}
//Returns in radians the arc sine of a numeric expression.
func Asin(zf float64) float64 {
return math.Asin(zf)
}
//This trigonometric function returns the tangent of an angle.
func Tan(zf float64) float64 {
return math.Tan(zf)
}
//Returns in radians the arc tangent of a numeric expression.
func Atan(zf float64) float64 {
return math.Atan(zf)
}
//Returns the square root of the specified numeric expression.
func Sqrt(zf float64) float64 {
return math.Sqrt(zf)
}
//Evaluates a set of expressions and returns the expression with the maximum value.
func Max(zf1, zf2 float64, zf_args ...float64) float64 {
if len(zf_args) == 0 {
return math.Max(zf1, zf2)
}
zv := math.Max(zf1, zf2)
for zi := 0; zi < len(zf_args); zi++ {
zv = math.Max(zv, zf_args[zi])
}
return zv
}
//Evaluates a set of expressions and returns the expression with the minimum value.
func Min(zf1, zf2 float64, zf_args ...float64) float64 {
if len(zf_args) == 0 {
return math.Max(zf1, zf2)
}
zv := math.Min(zf1, zf2)
for zi := 0; zi < len(zf_args); zi++ {
zv = math.Min(zv, zf_args[zi])
}
return zv
}
//Totals all on numeric array
func Sum(zf_args []float64) float64 {
if len(zf_args) == 0 {
return 0
}
var zv float64 = 0
for zi := 0; zi < len(zf_args); zi++ {
zv = zv + zf_args[zi]
}
return zv
}
//Computes the arithmetic average of numeric expressions
func Avg(zf_args []float64) float64 {
if len(zf_args) == 0 {
return 0
}
return Sum(zf_args) / float64(len(zf_args))
}
func Val(zd string) float64 {
zr, _ := strconv.ParseFloat(zd, 64)
return zr
}
//Returns the next highest integer that is greater than or equal to the specified numeric expression.
/*
Ceiling(10.9) // Displays 11
Ceiling(-10.9) // Displays -10
Ceiling(10.0) // Displays 10
Ceiling(-10.0) // Displays -10
*/
func Ceiling(z float64) float64 {
if z > 0 && z > float64(Int(z)) {
return float64(Int(z) + 1)
}
return float64(Int(z))
}
//Returns a numeric value of 1, –1, or 0 if the specified numeric expression evaluates to a positive, negative, or 0 value.
func Sign(z interface{}) int {
zv := Val(fmt.Sprintf("%v", z))
if zv > 0 {
return 1
}
if zv < 0 {
return -1
}
return 0
}
//Returns the nearest integer that is less than or equal to the specified numeric expression.
/*
Floor(10.9) // Displays 10
Floor(-10.9) // Displays -11
Floor(10.0) // Displays 10
Floor(-10.0) // Displays -10
*/
func Floor(z float64) float64 {
if z < 0 && z < float64(Int(z)) {
return float64(Int(z) - 1)
}
return float64(Int(z))
}
//Returns a numeric expression rounded to a specified number of decimal places.
/*Example
fmt.Println("round:",
vfp.Round(10.4545, 2),
vfp.Round(10.4545, 1),
vfp.Round(-10.5545, 0))//round: 10.45 10.5 -11
*/
func Round(zval float64, zdecimal float64) float64 {
zsign := Sign(zval)
zval = Abs(zval)
zseed := float64(math.Pow(10, zdecimal))
zval = zval * zseed
zint := int(zval)
if zval-float64(zint) >= 0.5 {
zval += 1
}
zval = float64(int(zval))
zval /= zseed
return zval * float64(zsign)
}