-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
61 lines (54 loc) · 881 Bytes
/
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
package do
import (
"math"
"math/big"
)
func Round(x float64, p int) float64 {
if p == 0 {
return math.Round(x)
}
var n = 1.0
for i := 0; i < p; i++ {
n *= 10
}
return math.Round(x*n) / n
}
func Floor(x float64, p int) float64 {
if p == 0 {
return math.Floor(x)
}
var n = 1.0
for i := 0; i < p; i++ {
n *= 10
}
return math.Floor(x*n) / n
}
func Ceil(x float64, p int) float64 {
if p == 0 {
return math.Ceil(x)
}
var n = 1.0
for i := 0; i < p; i++ {
n *= 10
}
return math.Ceil(x*n) / n
}
// Factorial n <= 20, it will return 0 if n > 20, use FactorialBig instead.
func Factorial(n int) int {
if n > 20 {
return 0
}
s := 1
for i := 2; i <= n; i++ {
s *= i
}
return s
}
// FactorialBig n > 20
func FactorialBig(n int) string {
s := big.NewInt(1)
for i := 2; i <= n; i++ {
s = s.Mul(s, big.NewInt(int64(i)))
}
return s.String()
}