-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.go
83 lines (71 loc) · 1.56 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
package slang
// Add adds given floating point numbers and returns the sum.
func Add(args ...float64) float64 {
sum := 0.0
for _, a := range args {
sum += a
}
return sum
}
// Sub subtracts args from 'x' and returns the final result.
func Sub(x float64, args ...float64) float64 {
if len(args) == 0 {
return -1 * x
}
for _, a := range args {
x -= a
}
return x
}
// Multiply multiplies the given args to 1 and returns the result.
func Multiply(args ...float64) float64 {
p := 1.0
for _, a := range args {
p *= a
}
return p
}
// Divide returns the product of given numbers.
func Divide(first float64, args ...float64) float64 {
if len(args) == 0 {
return 1 / first
}
for _, a := range args {
first /= a
}
return first
}
// Lt returns true if the given args are monotonically increasing.
func Lt(base float64, args ...float64) bool {
inc := true
for _, arg := range args {
inc = inc && (arg > base)
}
return inc
}
// LtE returns true if the given args are monotonically increasing or
// are all equal.
func LtE(base float64, args ...float64) bool {
inc := true
for _, arg := range args {
inc = inc && (arg >= base)
}
return inc
}
// Gt returns true if the given args are monotonically decreasing.
func Gt(base float64, args ...float64) bool {
inc := true
for _, arg := range args {
inc = inc && (arg < base)
}
return inc
}
// GtE returns true if the given args are monotonically decreasing or
// all equal.
func GtE(base float64, args ...float64) bool {
inc := true
for _, arg := range args {
inc = inc && (arg <= base)
}
return inc
}