-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbig_functions.go
60 lines (48 loc) · 1.31 KB
/
big_functions.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
package mathcat
import "math/big"
// RationalToInteger converts a rational number to an integer
func RationalToInteger(n *big.Rat) *big.Int {
return new(big.Int).Div(n.Num(), n.Denom())
}
// Factorial calculates the factorial of rational number n
func Factorial(n *big.Rat) *big.Rat {
integer := RationalToInteger(n)
fact := new(big.Int).MulRange(1, integer.Int64())
return new(big.Rat).SetInt(fact)
}
// Gcd calculates the greatest common divisor of the numbers x and y
func Gcd(x, y *big.Rat) *big.Rat {
xInt := RationalToInteger(x)
yInt := RationalToInteger(y)
gcd := new(big.Int).GCD(nil, nil, xInt, yInt)
return new(big.Rat).SetInt(gcd)
}
// Max gives the maximum of two rational numbers
func Max(a, b *big.Rat) *big.Rat {
if a.Cmp(b) == 1 {
return a
}
return b
}
// Min gives the minimum of two rational numbers
func Min(a, b *big.Rat) *big.Rat {
if a.Cmp(b) == -1 {
return a
}
return b
}
// Floor returns the floor of a rational number
func Floor(n *big.Rat) *big.Rat {
return new(big.Rat).SetInt(RationalToInteger(n))
}
// Ceil returns the ceil of a rational number
func Ceil(n *big.Rat) *big.Rat {
floor := Floor(n.Neg(n))
return new(big.Rat).Neg(floor)
}
// Mod returns x % y
func Mod(x, y *big.Rat) *big.Rat {
res := new(big.Rat)
quo := Floor(res.Quo(x, y))
return res.Sub(x, res.Mul(y, quo))
}