forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction.go
152 lines (116 loc) · 3.05 KB
/
fraction.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
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package utils
import (
"math/big"
"strconv"
"strings"
)
const PRECISION = 100
// Fraction type
type Fraction struct {
n *big.Int
d *big.Int
}
var (
fraczero = big.NewInt(0)
one = big.NewInt(1)
baseF0 = NewFraction(fraczero, one)
baseF1 = NewFraction(one, one)
)
//const Precision = 16
// NewFraction from a numerator and denominator
func NewFraction(n *big.Int, d *big.Int) *Fraction {
return &Fraction{n, d}
}
// NewFractionFromMaterializedPath function
func NewFractionFromMaterializedPath(path ...uint64) *Fraction {
f := baseF0
var current big.Int
for i := len(path) - 1; i >= 0; i-- {
current.SetUint64(path[i])
f = add(baseF1, invert(f))
f = add(NewFraction(¤t, big.NewInt(1)), invert(f))
}
return f
}
// ToPath a Fraction
func ToPath(f *Fraction) string {
/*
var r big.Rat
var c []int64
for f.n.Cmp(zero) == 1 {
r.SetFrac(f.n, f.d)
f64, _ := r.Float64()
i := int64(f64)
c = append(c, i)
f = invert(subtract(f, NewFraction(big.NewInt(i), one)))
f = invert(subtract(f, baseF1))
}
*/
c := ToPathUint(f)
if len(c) == 0 {
return ""
}
b := make([]string, len(c))
for i, v := range c {
b[i] = strconv.Itoa(int(v))
}
return strings.Join(b, ".")
}
func ToPathUint(f *Fraction) []uint64 {
var r big.Rat
var c []uint64
for f.n.Cmp(fraczero) == 1 {
r.SetFrac(f.n, f.d)
f64, _ := r.Float64()
i := int64(f64)
u := uint64(f64)
c = append(c, u)
f = invert(subtract(f, NewFraction(big.NewInt(i), one)))
f = invert(subtract(f, baseF1))
}
return c
}
// Decimal representation of the fraction
func (f Fraction) Decimal() *big.Rat {
var d big.Rat
d.SetFrac(f.n, f.d)
return &d
}
// Num value of the fraction
func (f Fraction) Num() *big.Int {
return f.n
}
// Den value of the fraction
func (f Fraction) Den() *big.Int {
return f.d
}
func add(f1 *Fraction, f2 *Fraction) *Fraction {
var add1, mul1, mul2, mul3 big.Int
return NewFraction(add1.Add(mul1.Mul(f1.n, f2.d), mul2.Mul(f2.n, f1.d)), mul3.Mul(f2.d, f1.d))
}
func subtract(f1 *Fraction, f2 *Fraction) *Fraction {
var sub1, mul1, mul2, mul3 big.Int
return NewFraction(sub1.Sub(mul1.Mul(f1.n, f2.d), mul2.Mul(f2.n, f1.d)), mul3.Mul(f2.d, f1.d))
}
func invert(f *Fraction) *Fraction {
return NewFraction(f.d, f.n)
}