-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhistogram_bounds.go
66 lines (56 loc) · 1.27 KB
/
histogram_bounds.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
package hrtime
import "math"
func calculateSteps(min, max float64, bincount int) (minimum, spacing float64) {
minimum = min
spacing = (max - min) / float64(bincount)
return minimum, spacing
}
func calculateNiceSteps(min, max float64, bincount int) (minimum, spacing float64) {
span := niceNumber(max-min, false)
spacing = niceNumber(span/float64(bincount-1), true)
minimum = math.Floor(min/spacing) * spacing
return minimum, spacing
}
func niceNumber(span float64, round bool) float64 {
exp := math.Floor(math.Log10(span))
frac := span / math.Pow(10, exp)
var nice float64
if round {
switch {
case frac < 1.5:
nice = 1
case frac < 3:
nice = 2
case frac < 7:
nice = 5
default:
nice = 10
}
} else {
switch {
case frac <= 1:
nice = 1
case frac <= 2:
nice = 2
case frac <= 5:
nice = 5
default:
nice = 10
}
}
return nice * math.Pow(10, exp)
}
func truncate(v float64, digits int) float64 {
if digits == 0 || v == 0 {
return 0
}
scale := math.Pow(10, math.Floor(math.Log10(v))+1-float64(digits))
return scale * math.Trunc(v/scale)
}
func round(v float64, digits int) float64 {
if digits == 0 || v == 0 {
return 0
}
scale := math.Pow(10, math.Floor(math.Log10(v))+1-float64(digits))
return scale * math.Round(v/scale)
}