forked from KirkMcDonald/satisfactory-calculator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rational.js
254 lines (240 loc) · 7.9 KB
/
rational.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*Copyright 2015-2019 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
"use strict"
export class Rational {
constructor(p, q) {
if (q.lesser(bigInt.zero)) {
p = bigInt.zero.minus(p)
q = bigInt.zero.minus(q)
}
var gcd = bigInt.gcd(p.abs(), q)
if (gcd.greater(bigInt.one)) {
p = p.divide(gcd)
q = q.divide(gcd)
}
this.p = p
this.q = q
}
toFloat() {
return this.p.toJSNumber() / this.q.toJSNumber()
}
toString() {
if (this.q.equals(bigInt.one)) {
return this.p.toString()
}
return this.p.toString() + "/" + this.q.toString()
}
toDecimal(maxDigits, roundingFactor) {
if (maxDigits == null) {
maxDigits = 3
}
if (roundingFactor == null) {
roundingFactor = new Rational(bigInt(5), bigInt(10).pow(maxDigits+1))
}
var sign = ""
var x = this
if (x.less(zero)) {
sign = "-"
x = zero.sub(x)
}
x = x.add(roundingFactor)
var divmod = x.p.divmod(x.q)
var integerPart = divmod.quotient.toString()
var decimalPart = ""
var fraction = new Rational(divmod.remainder, x.q)
var ten = new Rational(bigInt(10), bigInt.one)
while (maxDigits > 0 && !fraction.equal(roundingFactor)) {
fraction = fraction.mul(ten)
roundingFactor = roundingFactor.mul(ten)
divmod = fraction.p.divmod(fraction.q)
decimalPart += divmod.quotient.toString()
fraction = new Rational(divmod.remainder, fraction.q)
maxDigits--
}
if (fraction.equal(roundingFactor)) {
while (decimalPart[decimalPart.length - 1] == "0") {
decimalPart = decimalPart.slice(0, decimalPart.length - 1)
}
}
if (decimalPart != "") {
return sign + integerPart + "." + decimalPart
}
return sign + integerPart
}
toUpDecimal(maxDigits) {
var fraction = new Rational(bigInt.one, bigInt(10).pow(maxDigits))
var divmod = this.divmod(fraction)
var x = this
if (!divmod.remainder.isZero()) {
x = x.add(fraction)
}
return x.toDecimal(maxDigits, zero)
}
toMixed() {
var divmod = this.p.divmod(this.q)
if (divmod.quotient.isZero() || divmod.remainder.isZero()) {
return this.toString()
}
return divmod.quotient.toString() + " + " + divmod.remainder.toString() + "/" + this.q.toString()
}
isZero() {
return this.p.isZero()
}
isInteger() {
return this.q.equals(bigInt.one)
}
ceil() {
var divmod = this.p.divmod(this.q)
var result = new Rational(divmod.quotient, bigInt.one)
if (!divmod.remainder.isZero()) {
result = result.add(one)
}
return result
}
floor() {
var divmod = this.p.divmod(this.q)
var result = new Rational(divmod.quotient, bigInt.one)
if (result.less(zero) && !divmod.remainder.isZero()) {
result = result.sub(one)
}
return result
}
equal(other) {
return this.p.equals(other.p) && this.q.equals(other.q)
}
less(other) {
return this.p.times(other.q).lesser(this.q.times(other.p))
}
abs() {
if (this.less(zero)) {
return this.mul(minusOne)
}
return this
}
add(other) {
return new Rational(
this.p.times(other.q).plus(this.q.times(other.p)),
this.q.times(other.q)
)
}
sub(other) {
return new Rational(
this.p.times(other.q).subtract(this.q.times(other.p)),
this.q.times(other.q)
)
}
mul(other) {
return new Rational(
this.p.times(other.p),
this.q.times(other.q)
)
}
div(other) {
return new Rational(
this.p.times(other.q),
this.q.times(other.p)
)
}
divmod(other) {
var quotient = this.div(other)
var div = quotient.floor()
var mod = this.sub(other.mul(div))
return {quotient: div, remainder: mod}
}
reciprocate() {
return new Rational(this.q, this.p)
}
// exp must be a JS float with an integer in it.
pow(exp) {
return new Rational(this.p.pow(exp), this.q.pow(exp))
}
static from_decimal(s) {
let i = s.indexOf(".")
if (i === -1 || i === s.length - 1) {
return new Rational(bigInt(s), bigInt.one)
}
let integerPart = new Rational(bigInt(s.slice(0, i)), bigInt.one)
let numerator = bigInt(s.slice(i + 1))
let denominator = bigInt(10).pow(s.length - i - 1)
return integerPart.add(new Rational(numerator, denominator))
}
static from_string(s) {
var i = s.indexOf("/")
if (i === -1) {
return Rational.from_decimal(s)
}
var j = s.indexOf("+")
var q = bigInt(s.slice(i + 1))
if (j !== -1) {
var integer = bigInt(s.slice(0, j))
var p = bigInt(s.slice(j + 1, i)).plus(integer.times(q))
} else {
var p = bigInt(s.slice(0, i))
}
return new Rational(p, q)
}
static from_integer(x) {
return Rational.from_floats(x, 1)
}
static from_float(arg) {
if (arg === 0 || !Number.isFinite(arg) || Number.isNaN(arg)) {
return zero
}
if (Number.isInteger(arg)) {
return Rational.from_integer(arg)
}
let x = Math.abs(arg)
let exp = Math.max(-1023, Math.floor(Math.log2(x)) + 1)
let floatPart = x * Math.pow(2, -exp)
for (let i = 0; i < 300 && floatPart !== Math.floor(floatPart); i++) {
floatPart *= 2
exp--
}
let numerator = bigInt(floatPart)
let denominator = bigInt.one
if (exp > 0) {
numerator = numerator.shiftLeft(exp)
} else {
denominator = denominator.shiftLeft(-exp)
}
return new Rational(numerator, denominator)
}
// This function is a hack, which intentionally limits its precision
// in order to paper over floating-point inaccuracies.
static from_float_approximate(x) {
if (Number.isInteger(x)) {
return Rational.from_floats(x, 1)
}
// Sufficient precision for our data?
var r = new Rational(bigInt(Math.round(x * 100000)), bigInt(100000))
// Recognize 1/3 and 2/3 explicitly.
var divmod = r.divmod(one)
if (divmod.remainder.equal(_one_third)) {
return divmod.quotient.add(oneThird)
} else if (divmod.remainder.equal(_two_thirds)) {
return divmod.quotient.add(twoThirds)
}
return r
}
static from_floats(p, q) {
return new Rational(bigInt(p), bigInt(q))
}
}
// Decimal approximations.
var _one_third = new Rational(bigInt(33333), bigInt(100000))
var _two_thirds = new Rational(bigInt(33333), bigInt(50000))
var minusOne = new Rational(bigInt.minusOne, bigInt.one)
var zero = new Rational(bigInt.zero, bigInt.one)
var one = new Rational(bigInt.one, bigInt.one)
var half = new Rational(bigInt.one, bigInt(2))
var oneThird = new Rational(bigInt.one, bigInt(3))
var twoThirds = new Rational(bigInt(2), bigInt(3))
export { minusOne, zero, one, half, oneThird, twoThirds }