/
rational.gr
284 lines (270 loc) 路 6.54 KB
/
rational.gr
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
/**
* Utilities for working with the Rational type.
* @example from "rational" include Rational
*
* @example 1/2r
* @example 3/4r
*
* @since v0.6.0
*/
module Rational
from "runtime/unsafe/wasmi32" include WasmI32
from "runtime/exception" include Exception
from "runtime/numbers" include Numbers
use Numbers.{
coerceNumberToRational as fromNumber,
coerceRationalToNumber as toNumber,
rationalNumerator as numerator,
rationalDenominator as denominator,
addSubRational,
timesDivideRational,
rationalsEqual,
cmpRationals,
isInteger,
}
provide { fromNumber, toNumber, numerator, denominator }
/**
* Gets the numerator and denominator of the rational.
*
* @param x: The rational to split
* @returns The numerator and denominator of the rational
*
* @example Rational.toIntegerRatio(1/2r) == (1, 2)
* @example Rational.toIntegerRatio(2/8r) == (1, 4)
*
* @since v0.6.0
*/
provide let toIntegerRatio = x => {
(numerator(x), denominator(x))
}
/**
* Creates a rational from a numerator and denominator.
*
* @param numerator: The numerator
* @param denominator: The denominator
* @returns The reduced rational
*
* @throws InvalidArgument(String): If the numerator is not an integer
* @throws InvalidArgument(String): If the denominator is not an integer
*
* @example Rational.fromIntegerRatio(1, 2) == 1/2r
* @example Rational.fromIntegerRatio(2, 8) == 1/4r
*
* @since v0.6.0
*/
@unsafe
provide let fromIntegerRatio = (numerator: Number, denominator: Number) => {
let numeratorptr = WasmI32.fromGrain(numerator)
let denominatorptr = WasmI32.fromGrain(denominator)
if (!isInteger(numeratorptr)) {
throw Exception.InvalidArgument("Numerator must be an integer")
}
if (!isInteger(denominatorptr)) {
throw Exception.InvalidArgument("Denominator must be an integer")
}
fromNumber(numerator / denominator)
}
/**
* Computes the sum of its operands.
*
* @param x: The first operand
* @param y: The second operand
* @returns The sum of the two operands
*
* @example
* use Rational.{ (+) }
* assert 1/2r + 1/4r == 3/4r
*
* @since v0.6.0
*/
@unsafe
provide let (+) = (x: Rational, y: Rational) => {
let xptr = WasmI32.fromGrain(x)
let yptr = WasmI32.fromGrain(y)
let ptr = addSubRational(xptr, yptr, false, true)
WasmI32.toGrain(ptr): Rational
}
/**
* Computes the difference of its operands.
*
* @param x: The first operand
* @param y: The second operand
* @returns The difference of the two operands
*
* @example
* use Rational.{ (-) }
* assert 1/2r - 1/4r == 1/4r
*
* @since v0.6.0
*/
@unsafe
provide let (-) = (x: Rational, y: Rational) => {
let xptr = WasmI32.fromGrain(x)
let yptr = WasmI32.fromGrain(y)
let ptr = addSubRational(xptr, yptr, true, true)
WasmI32.toGrain(ptr): Rational
}
/**
* Computes the product of its operands.
*
* @param x: The first operand
* @param y: The second operand
* @returns The product of the two operands
*
* @example
* use Rational.{ (*) }
* assert 1/2r * 1/4r == 1/8r
*
* @since v0.6.0
*/
@unsafe
provide let (*) = (x: Rational, y: Rational) => {
let xptr = WasmI32.fromGrain(x)
let yptr = WasmI32.fromGrain(y)
let ptr = timesDivideRational(xptr, yptr, false, true)
WasmI32.toGrain(ptr): Rational
}
/**
* Computes the quotient of its operands.
*
* @param x: The first operand
* @param y: The second operand
* @returns The quotient of the two operands
*
* @example
* use Rational.{ (/) }
* assert 1/2r / 1/4r == 2/1r
*
* @since v0.6.0
*/
@unsafe
provide let (/) = (x: Rational, y: Rational) => {
let xptr = WasmI32.fromGrain(x)
let yptr = WasmI32.fromGrain(y)
let ptr = timesDivideRational(xptr, yptr, true, true)
WasmI32.toGrain(ptr): Rational
}
/**
* Checks if the first value is equal to the second value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is equal to the second value or `false` otherwise
*
* @example
* use Rational.{ (==) }
* assert 1/2r == 1/2r
*
* @since v0.6.0
*/
@unsafe
provide let (==) = (x: Rational, y: Rational) => {
let x = WasmI32.fromGrain(x)
let y = WasmI32.fromGrain(y)
rationalsEqual(x, y)
}
/**
* Checks if the first value is not equal to the second value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is not equal to the second value or `false` otherwise
*
* @example
* use Rational.{ (!=) }
* assert 1/2r != 1/4r
*
* @since v0.6.0
*/
@unsafe
provide let (!=) = (x: Rational, y: Rational) => {
let x = WasmI32.fromGrain(x)
let y = WasmI32.fromGrain(y)
!rationalsEqual(x, y)
}
/**
* Checks if the first value is less than the second value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is less than the second value or `false` otherwise
*
* @example
* use Rational.{ (<) }
* assert 1/4r < 1/2r
*
* @since v0.6.0
*/
@unsafe
provide let (<) = (x: Rational, y: Rational) => {
use WasmI32.{ (<) }
let x = WasmI32.fromGrain(x)
let y = WasmI32.fromGrain(y)
cmpRationals(x, y) < 0n
}
/**
* Checks if the first value is greater than the second value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is greater than the second value or `false` otherwise
*
* @example
* use Rational.{ (>) }
* assert 1/2r > 1/4r
*
* @since v0.6.0
*/
@unsafe
provide let (>) = (x: Rational, y: Rational) => {
use WasmI32.{ (>) }
let x = WasmI32.fromGrain(x)
let y = WasmI32.fromGrain(y)
cmpRationals(x, y) > 0n
}
/**
* Checks if the first value is less than or equal to the second value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
*
* @example
* use Rational.{ (<=) }
* assert 1/4r <= 1/2r
* @example
* use Rational.{ (<=) }
* assert 1/2r <= 1/2r
*
* @since v0.6.0
*/
@unsafe
provide let (<=) = (x: Rational, y: Rational) => {
use WasmI32.{ (<=) }
let x = WasmI32.fromGrain(x)
let y = WasmI32.fromGrain(y)
cmpRationals(x, y) <= 0n
}
/**
* Checks if the first value is greater than or equal to the second value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
*
* @example
* use Rational.{ (>=) }
* assert 1/2r >= 1/4r
* @example
* use Rational.{ (>=) }
* assert 1/2r >= 1/2r
*
* @since v0.6.0
*/
@unsafe
provide let (>=) = (x: Rational, y: Rational) => {
use WasmI32.{ (>=) }
let x = WasmI32.fromGrain(x)
let y = WasmI32.fromGrain(y)
cmpRationals(x, y) >= 0n
}