-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmath.go
163 lines (153 loc) · 3.06 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
// SPDX-FileCopyrightText: 2022 Kalle Fagerberg
//
// SPDX-License-Identifier: MIT
package typ
// Min returns the smallest value.
func Min[T Ordered](v ...T) T {
switch len(v) {
case 0:
panic("typ.Min: at least one argument is required")
case 1:
return v[0]
default:
min := v[0]
for _, v := range v[1:] {
if v < min {
min = v
}
}
return min
}
}
// Max returns the largest value.
func Max[T Ordered](v ...T) T {
switch len(v) {
case 0:
panic("typ.Max: at least one argument is required")
case 1:
return v[0]
default:
max := v[0]
for _, v := range v[1:] {
if v > max {
max = v
}
}
return max
}
}
// Clamp returns the value clamped between the minimum and maximum values.
func Clamp[T Ordered](v, min, max T) T {
if v < min {
return min
}
if v > max {
return max
}
return v
}
// Clamp01 returns the value clamped between 0 (zero) and 1 (one).
func Clamp01[T Real](v T) T {
if v < 0 {
return 0
}
if v > 1 {
return 1
}
return v
}
// Sum adds upp all numbers from the arguments. Returns 0 if no arguments.
func Sum[T Number](v ...T) T {
var sum T
for _, num := range v {
sum += num
}
return sum
}
// Product multiplies together all numbers from the arguments. Returns 1 if no
// arguments.
func Product[T Number](v ...T) T {
var product T = 1
for _, num := range v {
product *= num
}
return product
}
// Abs returns the absolute value of a number, in other words removing the sign,
// in other words (again) changing negative numbers to positive and leaving
// positive numbers as-is.
// Abs(0) // => 0
// Abs(15) // => 15
// Abs(-15) // => 15
func Abs[T Real](v T) T {
if v < 0 {
return -v
}
return v
}
// DigitsSign10 returns the number of digits in the number as if it would be
// converted to a string in base 10, plus 1 if the number is negative to account
// for the negative sign. This is computed by comparing its value to all orders
// of 10, making it increadibly faster than calculating logaritms or by
// performing divisions.
func DigitsSign10[T Integer](v T) int {
if v < 0 {
return Digits10(-v) + 1
}
return Digits10(v)
}
// Digits10 returns the number of digits in the number as if it would be
// converted to a string in base 10. This is computed by comparing its value
// to all orders of 10, making it increadibly faster than calculating logaritms
// or by performing divisions.
func Digits10[T Integer](v T) int {
if v < 0 {
v = -v
}
n := uint64(v)
switch {
case n < 10:
return 1
case n < 1e2:
return 2
case n < 1e3:
return 3
case n < 1e4:
return 4
case n < 1e5:
return 5
case n < 1e6:
return 6
case n < 1e7:
return 7
case n < 1e8:
return 8
case n < 1e9:
return 9
case n < 1e10:
return 10
case n < 1e11:
return 11
case n < 1e12:
return 12
case n < 1e13:
return 13
case n < 1e14:
return 14
case n < 1e15:
return 15
case n < 1e16:
return 16
case n < 1e17:
return 17
case n < 1e18:
return 18
case n < 1e19:
return 19
default:
// largest uint64 is 20 digits long.
// 18446744073709551615 <- max uint64
// 01234567890123456789
return 20
}
}