-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
big_rational.cr
352 lines (289 loc) · 6.89 KB
/
big_rational.cr
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require "big"
# Rational numbers are represented as the quotient of arbitrarily large
# numerators and denominators. Rationals are canonicalized such that the
# denominator and the numerator have no common factors, and that the
# denominator is positive. Zero has the unique representation 0/1.
#
# ```
# require "big"
#
# r = BigRational.new(7.to_big_i, 3.to_big_i)
# r.to_s # => "7/3"
#
# r = BigRational.new(3, -9)
# r.to_s # => "-1/3"
# ```
#
# It is implemented under the hood with [GMP](https://gmplib.org/).
struct BigRational < Number
include Comparable(BigRational)
include Comparable(Int)
include Comparable(Float)
private MANTISSA_BITS = 53
private MANTISSA_SHIFT = (1_i64 << MANTISSA_BITS).to_f64
# Creates a new `BigRational`.
#
# If *denominator* is 0, this will raise an exception.
def initialize(numerator : Int, denominator : Int)
check_division_by_zero denominator
numerator = BigInt.new(numerator) unless numerator.is_a?(BigInt)
denominator = BigInt.new(denominator) unless denominator.is_a?(BigInt)
LibGMP.mpq_init(out @mpq)
LibGMP.mpq_set_num(mpq, numerator.to_unsafe)
LibGMP.mpq_set_den(mpq, denominator.to_unsafe)
LibGMP.mpq_canonicalize(mpq)
end
# Creates a new `BigRational` with *num* as the numerator and 1 for denominator.
def initialize(num : Int)
initialize(num, 1)
end
# Creates a exact representation of float as rational.
def initialize(num : Float)
# It ensures that `BigRational.new(f) == f`
# It relies on fact, that mantissa is at most 53 bits
frac, exp = Math.frexp num
ifrac = (frac.to_f64 * MANTISSA_SHIFT).to_i64
exp -= MANTISSA_BITS
initialize ifrac, 1
if exp >= 0
LibGMP.mpq_mul_2exp(out @mpq, self, exp)
else
LibGMP.mpq_div_2exp(out @mpq, self, -exp)
end
end
# :nodoc:
def initialize(@mpq : LibGMP::MPQ)
end
# :nodoc:
def self.new
LibGMP.mpq_init(out mpq)
yield pointerof(mpq)
new(mpq)
end
def numerator
BigInt.new { |mpz| LibGMP.mpq_get_num(mpz, self) }
end
def denominator
BigInt.new { |mpz| LibGMP.mpq_get_den(mpz, self) }
end
def <=>(other : BigRational)
LibGMP.mpq_cmp(mpq, other)
end
def <=>(other : Float32 | Float64)
self <=> BigRational.new(other)
end
def <=>(other : Float)
to_big_f <=> other.to_big_f
end
def <=>(other : Int)
LibGMP.mpq_cmp(mpq, other.to_big_r)
end
def +(other : BigRational)
BigRational.new { |mpq| LibGMP.mpq_add(mpq, self, other) }
end
def +(other : Int)
self + other.to_big_r
end
def -(other : BigRational)
BigRational.new { |mpq| LibGMP.mpq_sub(mpq, self, other) }
end
def -(other : Int)
self - other.to_big_r
end
def *(other : BigRational)
BigRational.new { |mpq| LibGMP.mpq_mul(mpq, self, other) }
end
def *(other : Int)
self * other.to_big_r
end
def /(other : BigRational)
check_division_by_zero other
BigRational.new { |mpq| LibGMP.mpq_div(mpq, self, other) }
end
def /(other : Int)
self / other.to_big_r
end
def //(other)
(self / other).floor
end
def ceil
diff = (denominator - numerator % denominator) % denominator
BigRational.new(numerator + diff, denominator)
end
def floor
BigRational.new(numerator - numerator % denominator, denominator)
end
def trunc
self < 0 ? ceil : floor
end
# Divides the rational by (2 ** *other*)
#
# ```
# require "big"
#
# BigRational.new(2, 3) >> 2 # => 1/6
# ```
def >>(other : Int)
BigRational.new { |mpq| LibGMP.mpq_div_2exp(mpq, self, other) }
end
# Multiplies the rational by (2 ** *other*)
#
# ```
# require "big"
#
# BigRational.new(2, 3) << 2 # => 8/3
# ```
def <<(other : Int)
BigRational.new { |mpq| LibGMP.mpq_mul_2exp(mpq, self, other) }
end
def -
BigRational.new { |mpq| LibGMP.mpq_neg(mpq, self) }
end
# Returns a new `BigRational` as 1/r.
#
# This will raise an exception if rational is 0.
def inv
check_division_by_zero self
BigRational.new { |mpq| LibGMP.mpq_inv(mpq, self) }
end
def abs
BigRational.new { |mpq| LibGMP.mpq_abs(mpq, self) }
end
# TODO: improve this
def_hash to_f64
# Returns the `Float64` representing this rational.
def to_f
to_f64
end
def to_f32
to_f64.to_f32
end
def to_f64
LibGMP.mpq_get_d(mpq)
end
def to_f32!
to_f64.to_f32!
end
def to_f64!
to_f64
end
def to_f!
to_f64!
end
def to_big_f
BigFloat.new { |mpf| LibGMP.mpf_set_q(mpf, mpq) }
end
# Returns the string representing this rational.
#
# Optionally takes a radix base (2 through 36).
#
# ```
# require "big"
#
# r = BigRational.new(8243243, 562828882)
# r.to_s # => "8243243/562828882"
# r.to_s(16) # => "7dc82b/218c1652"
# r.to_s(36) # => "4woiz/9b3djm"
# ```
def to_s(base : Int = 10) : String
String.new(to_cstr(base))
end
def to_s(io : IO, base : Int = 10) : Nil
str = to_cstr(base)
io.write_utf8 Slice.new(str, LibC.strlen(str))
end
def inspect : String
to_s
end
def inspect(io : IO) : Nil
to_s io
end
def clone
self
end
private def mpq
pointerof(@mpq)
end
def to_unsafe
mpq
end
private def to_cstr(base = 10)
raise "Invalid base #{base}" unless 2 <= base <= 36
LibGMP.mpq_get_str(nil, base, mpq)
end
private def check_division_by_zero(value)
raise DivisionByZeroError.new if value == 0
end
end
struct Int
include Comparable(BigRational)
# Returns a `BigRational` representing this integer.
# ```
# require "big"
#
# 123.to_big_r
# ```
def to_big_r
BigRational.new(self, 1)
end
def <=>(other : BigRational)
-(other <=> self)
end
def +(other : BigRational)
other + self
end
def -(other : BigRational)
self.to_big_r - other
end
def /(other : BigRational)
self.to_big_r / other
end
def //(other : BigRational)
self.to_big_r // other
end
def *(other : BigRational)
other * self
end
end
struct Float
include Comparable(BigRational)
# Returns a `BigRational` representing this float.
# ```
# require "big"
#
# 123.0.to_big_r
# ```
def to_big_r
BigRational.new(self)
end
def <=>(other : BigRational)
-(other <=> self)
end
end
module Math
# Returns the sqrt of a `BigRational`.
# ```
# require "big"
#
# Math.sqrt((1000_000_000_0000.to_big_r*1000_000_000_00000.to_big_r))
# ```
def sqrt(value : BigRational)
sqrt(value.to_big_f)
end
end
# :nodoc:
struct Crystal::Hasher
private HASH_MODULUS_RAT_P = BigRational.new((1_u64 << HASH_BITS) - 1)
private HASH_MODULUS_RAT_N = -BigRational.new((1_u64 << HASH_BITS) - 1)
def float(value : BigRational)
rem = value
if value >= HASH_MODULUS_RAT_P || value <= HASH_MODULUS_RAT_N
num = value.numerator
denom = value.denominator
div = num.tdiv(denom)
floor = div.tdiv(HASH_MODULUS)
rem -= floor * HASH_MODULUS
end
rem.to_big_f.hash
end
end