/
big_rational.cr
232 lines (185 loc) · 4.63 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
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.
#
# r = BigRational.new(BigInt.new(7),BigInt.new(3))
# 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)
# Create 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
# :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 : Float)
self.to_f <=> other
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
# Divides the rational by (2**`other`)
#
# 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`)
#
# 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
def hash
to_f64.hash
end
# 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
# Returns the string representing this rational.
#
# Optionally takes a radix base (2 through 36).
#
# 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 = 10)
String.new(to_cstr(base))
end
def to_s(io : IO, base = 10)
str = to_cstr(base)
io.write_utf8 Slice.new(str, LibC.strlen(str))
end
def inspect
to_s
end
def inspect(io)
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 DivisionByZero.new if value == 0
end
end
struct Int
include Comparable(BigRational)
# Returns a BigRational representing this integer.
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)
other * self
end
end
struct Float
include Comparable(BigRational)
def <=>(other : BigRational)
-(other <=> self)
end
end