-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath.go
More file actions
39 lines (33 loc) · 852 Bytes
/
math.go
File metadata and controls
39 lines (33 loc) · 852 Bytes
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
package gosprite64
import (
"math"
"math/rand/v2"
)
// Number is a constraint that permits any numeric type.
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64
}
// Flr returns the floor of a generic number.
// It works with any numeric type.
//
// Example:
//
// Flr(3.14) // returns 3
// Flr(-2.71) // returns -3
func Flr[T Number](a T) int {
floatVal := float64(a)
floorVal := math.Floor(floatVal)
return int(floorVal)
}
func Rnd[T Number](a T) int {
limit := float64(a)
if limit <= 0 {
return 0
}
// rand.Float64() returns a float64 in [0.0, 1.0)
// Multiplying by limit gives a float64 in [0.0, limit)
// Applying Floor and converting to int gives an integer in [0, floor(limit))
return int(math.Floor(rand.Float64() * limit))
}