@pkmn/engine 0.1.0-dev.15f64810 → 0.1.0-dev.18478e7d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +30 -17
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +26 -4
  13. package/build/pkg/addon.js +34 -65
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +23 -1
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +40 -90
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +29 -1
  26. package/build/pkg/protocol.js +105 -103
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +83 -73
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +66 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +360 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +157 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +346 -128
  39. package/package.json +59 -138
  40. package/src/bin/install-pkmn-engine +25 -7
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +201 -0
  43. package/src/lib/bindings/node.zig +117 -0
  44. package/src/lib/bindings/wasm.zig +51 -0
  45. package/src/lib/common/array.zig +70 -0
  46. package/src/lib/common/data.zig +8 -8
  47. package/src/lib/common/debug.zig +45 -29
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +26 -22
  50. package/src/lib/common/options.zig +4 -0
  51. package/src/lib/common/protocol.zig +196 -160
  52. package/src/lib/common/rational.zig +338 -254
  53. package/src/lib/common/rng.zig +5 -7
  54. package/src/lib/common/util.zig +53 -1
  55. package/src/lib/gen1/calc.zig +651 -205
  56. package/src/lib/gen1/chance.zig +443 -257
  57. package/src/lib/gen1/data/moves.zig +2 -4
  58. package/src/lib/gen1/data/species.zig +1 -3
  59. package/src/lib/gen1/data/types.zig +3 -4
  60. package/src/lib/gen1/data.zig +40 -43
  61. package/src/lib/gen1/helpers.zig +39 -574
  62. package/src/lib/gen1/mechanics.zig +328 -229
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +117 -518
  65. package/src/lib/gen2/data/items.zig +1 -3
  66. package/src/lib/gen2/data/moves.zig +2 -4
  67. package/src/lib/gen2/data/species.zig +1 -3
  68. package/src/lib/gen2/data/types.zig +7 -5
  69. package/src/lib/gen2/data.zig +33 -35
  70. package/src/lib/gen2/generated.zig +18 -23
  71. package/src/lib/gen2/helpers.zig +32 -33
  72. package/src/lib/gen2/mechanics.zig +105 -132
  73. package/src/lib/napi.h +2 -0
  74. package/src/lib/node.zig +4 -121
  75. package/src/lib/pkmn.zig +103 -22
  76. package/src/lib/wasm.zig +9 -9
  77. package/src/tools/display/dom.ts +108 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -8,9 +8,27 @@ const expectError = std.testing.expectError;
8
8
 
9
9
  /// Specialization of a rational number used by the engine to compute probabilties.
10
10
  /// For performance reasons the rational is only reduced lazily and thus `reduce` must be
11
- /// invoked explicitly before reading.
11
+ /// invoked explicitly before reading. Note that this laziness means that this implementation
12
+ /// sometimes overflows in places where an eager reduced implementation would not - to achieve
13
+ /// the same behavior as an eager implementation simply call `reduce` after each operation.
12
14
  pub fn Rational(comptime T: type) type {
13
- return extern struct {
15
+ // With floats we can't rely on an overflow bit to let us know when to reduce, so we
16
+ // instead start reducing when we get sufficiently close to the limit of the mantissa
17
+ // (in our domain we expect updates to involve numbers < 2**10, so we should be safe
18
+ // not reducing before we are 2**10 away from "overflowing" the mantissa)
19
+ const o = if (@typeInfo(T) == .float)
20
+ std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
21
+ else
22
+ 0;
23
+
24
+ const Err = switch (@typeInfo(T)) {
25
+ .int => error{Overflow},
26
+ .float => error{},
27
+ else => unreachable,
28
+ };
29
+
30
+ // Zig doesn't allow integers > u128 in an extern struct...
31
+ return if (@sizeOf(T) <= 16) extern struct {
14
32
  const Self = @This();
15
33
 
16
34
  /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
@@ -18,21 +36,8 @@ pub fn Rational(comptime T: type) type {
18
36
  /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
19
37
  q: T = 1,
20
38
 
21
- // With floats we can't rely on an overflow bit to let us know when to reduce, so we instead
22
- // start reducing when we get sufficiently close to the limit of the mantissa (in our domain
23
- // we expect updates to involve numbers < 2**10, so we should be safe not reducing before we
24
- // are 2**10 away from "overflowing" the mantissa)
25
- const REDUCE = if (@typeInfo(T) == .Float)
26
- std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
27
- else
28
- 0;
29
-
30
39
  /// Possible error returned by operations on the Rational.
31
- pub const Error = switch (@typeInfo(T)) {
32
- .Int => error{Overflow},
33
- .Float => error{},
34
- else => unreachable,
35
- };
40
+ pub const Error = Err;
36
41
 
37
42
  /// Resets the rational back to 1.
38
43
  pub fn reset(r: *Self) void {
@@ -43,181 +48,327 @@ pub fn Rational(comptime T: type) type {
43
48
  /// Update the rational by multiplying its numerator by p and its denominator by q.
44
49
  /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
45
50
  pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
46
- // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
47
- assert(p >= 1);
48
- assert(q >= 1);
49
-
50
- // If our parameters are not fully reduced they may prematurely
51
- // cause overflow/loss of precision after the multiplication below
52
- assert(switch (@typeInfo(@TypeOf(p, q))) {
53
- .ComptimeInt, .ComptimeFloat => comptime gcd(p, q),
54
- else => 1,
55
- } == 1);
56
-
57
- switch (@typeInfo(T)) {
58
- .Int => {
59
- // Greedily attempt to multiply and if it fails, reduce and try again
60
- r.multiplication(p, q) catch |err| switch (err) {
61
- error.Overflow => {
62
- r.reduce();
63
- try r.multiplication(p, q);
64
- },
65
- else => unreachable,
66
- };
67
- },
68
- .Float => {
69
- // Reduce in situations where we're likely to start losing precision
70
- if (r.q > REDUCE or r.p > REDUCE) r.reduce();
71
-
72
- r.p *= switch (@typeInfo(@TypeOf(p))) {
73
- .Float, .ComptimeFloat => p,
74
- else => @floatFromInt(p),
75
- };
76
- r.q *= switch (@typeInfo(@TypeOf(q))) {
77
- .Float, .ComptimeFloat => q,
78
- else => @floatFromInt(q),
79
- };
80
-
81
- // We should always be dealing with whole numbers
82
- assert(std.math.modf(r.p).fpart == 0);
83
- assert(std.math.modf(r.q).fpart == 0);
84
- },
85
- else => unreachable,
86
- }
51
+ return update_(T, o, r, p, q);
87
52
  }
88
53
 
89
54
  /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
90
- pub fn add(r: *Self, s: *Self) Error!void {
91
- switch (@typeInfo(T)) {
92
- .Int => {
93
- if (r.q == s.q) {
94
- r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
95
- error.Overflow => val: {
96
- r.reduce();
97
- s.reduce();
98
- break :val try std.math.add(T, r.p, s.p);
99
- },
100
- else => unreachable,
101
- };
102
- } else {
103
- r.addition(s.p, s.q) catch |err| switch (err) {
104
- error.Overflow => {
105
- r.reduce();
106
- s.reduce();
107
- try r.addition(s.p, s.q);
108
- },
109
- else => unreachable,
110
- };
111
- }
112
- },
113
- .Float => {
114
- if (r.q == s.q) {
115
- if (r.p > REDUCE) r.reduce();
116
- if (s.p > REDUCE) s.reduce();
117
-
118
- r.p += s.p;
119
- } else {
120
- // Always reduce to minimize loss of precision from the multiplications
121
- r.reduce();
122
- s.reduce();
123
-
124
- r.p = (r.p * s.q) + (r.q * s.p);
125
- r.q *= s.q;
126
- }
127
-
128
- assert(std.math.modf(r.p).fpart == 0);
129
- assert(std.math.modf(r.q).fpart == 0);
130
- },
131
- else => unreachable,
132
- }
55
+ pub fn add(r: *Self, s: anytype) Error!void {
56
+ return add_(T, o, r, s);
133
57
  }
134
58
 
135
59
  /// Multiplies two rationals.
136
- pub fn mul(r: *Self, s: *Self) Error!void {
137
- switch (@typeInfo(T)) {
138
- .Int => {
139
- r.multiplication(s.p, s.q) catch |err| switch (err) {
140
- error.Overflow => {
141
- r.reduce();
142
- s.reduce();
143
- try r.multiplication(s.p, s.q);
144
- },
145
- else => unreachable,
146
- };
147
- },
148
- .Float => {
149
- if (r.q > REDUCE or r.p > REDUCE) r.reduce();
150
- if (s.q > REDUCE or s.p > REDUCE) s.reduce();
151
-
152
- r.p *= s.p;
153
- r.q *= s.q;
60
+ pub fn mul(r: *Self, s: anytype) Error!void {
61
+ return mul_(T, o, r, s);
62
+ }
154
63
 
155
- assert(std.math.modf(r.p).fpart == 0);
156
- assert(std.math.modf(r.q).fpart == 0);
157
- },
158
- else => unreachable,
159
- }
64
+ /// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
65
+ pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
66
+ return cmp_(T, o, r, s);
160
67
  }
161
68
 
162
69
  /// Normalize the rational by reducing by the greatest common divisor.
163
70
  pub fn reduce(r: *Self) void {
164
- const d = gcd(r.p, r.q);
165
- if (d == 1) return;
71
+ reduce_(r);
72
+ }
73
+
74
+ pub fn format(self: Self, writer: *std.Io.Writer) !void {
75
+ try writer.print("{d}/{d}", .{ self.p, self.q });
76
+ }
77
+ } else struct {
78
+ const Self = @This();
166
79
 
167
- assert(@mod(r.p, d) == 0);
168
- assert(@mod(r.q, d) == 0);
80
+ /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
81
+ p: T = 1,
82
+ /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
83
+ q: T = 1,
169
84
 
170
- r.p /= d;
171
- r.q /= d;
85
+ /// Possible error returned by operations on the Rational.
86
+ pub const Error = Err;
172
87
 
173
- assert(r.p >= 1);
174
- assert(r.q >= 1);
88
+ /// Resets the rational back to 1.
89
+ pub fn reset(r: *Self) void {
90
+ r.p = 1;
91
+ r.q = 1;
175
92
  }
176
93
 
177
- pub fn format(
178
- self: Self,
179
- comptime fmt: []const u8,
180
- opts: std.fmt.FormatOptions,
181
- writer: anytype,
182
- ) !void {
183
- _ = .{ fmt, opts };
184
- try writer.print("{d}/{d}", .{ self.p, self.q });
94
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
95
+ /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
96
+ pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
97
+ return update_(T, o, r, p, q);
98
+ }
99
+
100
+ /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
101
+ pub fn add(r: *Self, s: anytype) Error!void {
102
+ return add_(T, o, r, s);
103
+ }
104
+
105
+ /// Multiplies two rationals.
106
+ pub fn mul(r: *Self, s: anytype) Error!void {
107
+ return mul_(T, o, r, s);
185
108
  }
186
109
 
187
- fn multiplication(r: *Self, p: anytype, q: anytype) !void {
188
- r.p = try std.math.mul(T, r.p, p);
189
- r.q = try std.math.mul(T, r.q, q);
110
+ /// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
111
+ pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
112
+ return cmp_(T, o, r, s);
190
113
  }
191
114
 
192
- fn addition(r: *Self, p: anytype, q: anytype) !void {
193
- // (a/b) + (c/d) = (ad+bc)/(bd)
194
- const d = try std.math.mul(T, r.q, q);
195
- const n1 = try std.math.mul(T, r.p, q);
196
- const n2 = try std.math.mul(T, r.q, p);
115
+ /// Normalize the rational by reducing by the greatest common divisor.
116
+ pub fn reduce(r: *Self) void {
117
+ reduce_(r);
118
+ }
197
119
 
198
- r.p = try std.math.add(T, n1, n2);
199
- r.q = d;
120
+ pub fn format(self: Self, writer: *std.Io.Writer) !void {
121
+ try writer.print("{d}/{d}", .{ self.p, self.q });
200
122
  }
201
123
  };
202
124
  }
203
125
 
204
- // https://en.wikipedia.org/wiki/Euclidean_algorithm
126
+ fn update_(comptime T: type, comptime o: comptime_int, r: anytype, p: anytype, q: anytype) !void {
127
+ // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
128
+ assert(p >= 1);
129
+ assert(q >= 1);
130
+
131
+ // If our parameters are not fully reduced they may prematurely
132
+ // cause overflow/loss of precision after the multiplication below
133
+ assert(switch (@typeInfo(@TypeOf(p, q))) {
134
+ .comptime_int, .comptime_float => comptime gcd(p, q),
135
+ else => 1,
136
+ } == 1);
137
+
138
+ switch (@typeInfo(T)) {
139
+ .int => {
140
+ // Greedily attempt to multiply and if it fails, reduce and try again
141
+ multiplication(T, r, p, q) catch |err| switch (err) {
142
+ error.Overflow => {
143
+ r.reduce();
144
+ try multiplication(T, r, p, q);
145
+ },
146
+ else => unreachable,
147
+ };
148
+ },
149
+ .float => {
150
+ // Reduce in situations where we're likely to start losing precision
151
+ if (r.q > o or r.p > o) r.reduce();
152
+
153
+ r.p *= switch (@typeInfo(@TypeOf(p))) {
154
+ .float, .comptime_float => p,
155
+ else => @floatFromInt(p),
156
+ };
157
+ r.q *= switch (@typeInfo(@TypeOf(q))) {
158
+ .float, .comptime_float => q,
159
+ else => @floatFromInt(q),
160
+ };
161
+
162
+ // We should always be dealing with whole numbers
163
+ assert(std.math.modf(r.p).fpart == 0);
164
+ assert(std.math.modf(r.q).fpart == 0);
165
+ },
166
+ else => unreachable,
167
+ }
168
+ }
169
+
170
+ fn add_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
171
+ switch (@typeInfo(T)) {
172
+ .int => {
173
+ if (r.q == s.q) {
174
+ r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
175
+ error.Overflow => val: {
176
+ r.reduce();
177
+ s.reduce();
178
+ break :val try std.math.add(T, r.p, s.p);
179
+ },
180
+ else => unreachable,
181
+ };
182
+ } else {
183
+ addition(T, r, s.p, s.q) catch |err| switch (err) {
184
+ error.Overflow => {
185
+ r.reduce();
186
+ s.reduce();
187
+ try addition(T, r, s.p, s.q);
188
+ },
189
+ else => unreachable,
190
+ };
191
+ }
192
+ },
193
+ .float => {
194
+ if (r.q == s.q) {
195
+ if (r.p > o) r.reduce();
196
+ if (s.p > o) s.reduce();
197
+
198
+ r.p += s.p;
199
+ } else {
200
+ // Always reduce to minimize loss of precision from the multiplications
201
+ r.reduce();
202
+ s.reduce();
203
+
204
+ r.p = (r.p * s.q) + (r.q * s.p);
205
+ r.q *= s.q;
206
+ }
207
+
208
+ assert(std.math.modf(r.p).fpart == 0);
209
+ assert(std.math.modf(r.q).fpart == 0);
210
+ },
211
+ else => unreachable,
212
+ }
213
+ }
214
+
215
+ fn mul_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
216
+ switch (@typeInfo(T)) {
217
+ .int => {
218
+ multiplication(T, r, s.p, s.q) catch |err| switch (err) {
219
+ error.Overflow => {
220
+ r.reduce();
221
+ s.reduce();
222
+ try multiplication(T, r, s.p, s.q);
223
+ },
224
+ else => unreachable,
225
+ };
226
+ },
227
+ .float => {
228
+ if (r.q > o or r.p > o) r.reduce();
229
+ if (s.q > o or s.p > o) s.reduce();
230
+
231
+ r.p *= s.p;
232
+ r.q *= s.q;
233
+
234
+ assert(std.math.modf(r.p).fpart == 0);
235
+ assert(std.math.modf(r.q).fpart == 0);
236
+ },
237
+ else => unreachable,
238
+ }
239
+ }
240
+
241
+ fn cmp_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !std.math.Order {
242
+ switch (@typeInfo(T)) {
243
+ .int => {
244
+ const ad = std.math.mul(T, r.p, s.q) catch |err| switch (err) {
245
+ error.Overflow => ad: {
246
+ r.reduce();
247
+ s.reduce();
248
+ break :ad try std.math.mul(T, r.p, s.q);
249
+ },
250
+ else => unreachable,
251
+ };
252
+ const bc = std.math.mul(T, r.q, s.p) catch |err| switch (err) {
253
+ error.Overflow => bc: {
254
+ r.reduce();
255
+ s.reduce();
256
+ break :bc try std.math.mul(T, r.q, s.p);
257
+ },
258
+ else => unreachable,
259
+ };
260
+
261
+ return std.math.order(ad, bc);
262
+ },
263
+ .float => {
264
+ if (r.q > o or r.p > o) r.reduce();
265
+ if (s.q > o or s.p > o) s.reduce();
266
+
267
+ const ad: T = r.p * s.q;
268
+ const bc: T = r.q * s.p;
269
+
270
+ return std.math.order(ad, bc);
271
+ },
272
+ else => unreachable,
273
+ }
274
+ }
275
+
276
+ fn reduce_(r: anytype) void {
277
+ const d = gcd(r.p, r.q);
278
+ if (d == 1) return;
279
+
280
+ assert(@mod(r.p, d) == 0);
281
+ assert(@mod(r.q, d) == 0);
282
+
283
+ r.p /= d;
284
+ r.q /= d;
285
+
286
+ assert(r.p >= 1);
287
+ assert(r.q >= 1);
288
+ }
289
+
290
+ fn multiplication(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
291
+ r.p = try std.math.mul(T, r.p, p);
292
+ r.q = try std.math.mul(T, r.q, q);
293
+ }
294
+
295
+ fn addition(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
296
+ // (a/b) + (c/d) = (ad+bc)/(bd)
297
+ const bd = try std.math.mul(T, r.q, q);
298
+ const ad = try std.math.mul(T, r.p, q);
299
+ const bc = try std.math.mul(T, r.q, p);
300
+
301
+ r.p = try std.math.add(T, ad, bc);
302
+ r.q = bd;
303
+ }
304
+
205
305
  fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
206
306
  assert(p >= 1);
207
307
  assert(q >= 1);
208
308
 
209
- var a = p;
210
- var b = q;
211
- var c: @TypeOf(p, q) = undefined;
309
+ // convert comptime_int to a sized integer within this function so that @ctz will work
310
+ const T = switch (@TypeOf(p, q)) {
311
+ comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
312
+ else => |U| U,
313
+ };
314
+
315
+ switch (@typeInfo(T)) {
316
+ .int => {
317
+ // std.math.gcd but without some checks because we have a stricter range
318
+ var x: T = @intCast(p);
319
+ var y: T = @intCast(q);
320
+
321
+ const xz = @ctz(x);
322
+ const yz = @ctz(y);
323
+ const shift = @min(xz, yz);
324
+ x >>= @intCast(xz);
325
+ y >>= @intCast(yz);
326
+
327
+ var diff = y -% x;
328
+ while (diff != 0) : (diff = y -% x) {
329
+ const zeros = @ctz(diff);
330
+ if (x > y) diff = -%diff;
331
+ y = @min(x, y);
332
+ x = diff >> @intCast(zeros);
333
+ }
334
+
335
+ const result = y << @intCast(shift);
336
+ assert(result > 0);
337
+ return result;
338
+ },
339
+ else => {
340
+ var a = p;
341
+ var b = q;
342
+ var c: T = undefined;
343
+
344
+ while (b != 0) {
345
+ c = b;
346
+ b = @mod(a, b);
347
+ a = c;
348
+ }
212
349
 
213
- while (b != 0) {
214
- c = b;
215
- b = @mod(a, b);
216
- a = c;
350
+ assert(a > 0);
351
+ return a;
352
+ },
217
353
  }
354
+ }
218
355
 
219
- assert(a > 0);
220
- return a;
356
+ test gcd {
357
+ var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
358
+ var random = prng.random();
359
+
360
+ for (0..1000) |_| {
361
+ const a = random.int(u32);
362
+ const b = random.int(u32);
363
+ if (a == 0 or b == 0) continue;
364
+
365
+ try expectEqual(
366
+ gcd(a, b),
367
+ @as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
368
+ );
369
+ }
370
+
371
+ try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
221
372
  }
222
373
 
223
374
  fn doTurn(r: anytype) !void {
@@ -229,8 +380,9 @@ fn doTurn(r: anytype) !void {
229
380
  }
230
381
 
231
382
  test Rational {
232
- inline for (.{ u64, u128, f64 }) |t| {
233
- var r: Rational(t) = .{};
383
+ inline for (.{ u64, u128, u256, f64 }) |t| {
384
+ const R = Rational(t);
385
+ var r: R = .{};
234
386
 
235
387
  var c: t = 128;
236
388
  _ = &c;
@@ -239,7 +391,7 @@ test Rational {
239
391
  try doTurn(&r);
240
392
 
241
393
  r.reduce();
242
- try expectEqual(Rational(t){ .p = 75383, .q = 35550920704 }, r);
394
+ try expectEqual(R{ .p = 75383, .q = 35550920704 }, r);
243
395
 
244
396
  try r.update(1, 4);
245
397
  if (t == u64) {
@@ -247,110 +399,42 @@ test Rational {
247
399
  } else {
248
400
  try doTurn(&r);
249
401
  r.reduce();
250
- try expectEqual(Rational(t){ .p = 5682596689, .q = 2527735925804191711232 }, r);
402
+ try expectEqual(R{ .p = 5682596689, .q = 2527735925804191711232 }, r);
251
403
  }
252
404
 
253
405
  r.reset();
254
406
 
255
- var s = Rational(t){ .p = 10, .q = 13 };
407
+ var s = R{ .p = 10, .q = 13 };
256
408
  try r.mul(&s);
257
- s = Rational(t){ .p = 3, .q = 4 };
409
+ s = R{ .p = 3, .q = 4 };
258
410
  try r.mul(&s);
259
- try expectEqual(Rational(t){ .p = 30, .q = 52 }, r);
411
+ try expectEqual(R{ .p = 30, .q = 52 }, r);
260
412
 
261
- s = Rational(t){ .p = 1, .q = 3 };
413
+ s = R{ .p = 1, .q = 3 };
262
414
  try r.add(&s);
263
415
  r.reduce();
264
- try expectEqual(Rational(t){ .p = 71, .q = 78 }, r);
265
- }
266
- }
267
-
268
- /// Adapter of std.math.big.Rational to the engines Rational interface.
269
- pub const BigRational = struct {
270
- /// The underlying rational number.
271
- val: std.math.big.Rational,
272
-
273
- /// Possible error returned by operations on the rational.
274
- pub const Error = error{OutOfMemory};
416
+ try expectEqual(R{ .p = 71, .q = 78 }, r);
275
417
 
276
- /// Create a new BigRational wrapper. A small amount of memory will be allocated on
277
- /// initialization. Not default initialized to 1 - you must explicitly `reset` first.
278
- pub fn init(alloc: std.mem.Allocator) !BigRational {
279
- return .{ .val = try std.math.big.Rational.init(alloc) };
280
- }
281
-
282
- /// Frees all memory associated with a rational.
283
- pub fn deinit(r: *BigRational) void {
284
- r.val.deinit();
285
- }
286
-
287
- /// Resets the rational back to 1.
288
- pub fn reset(r: *BigRational) !void {
289
- try r.val.setInt(1);
290
- }
291
-
292
- /// Update the rational by multiplying its numerator by p and its denominator by q.
293
- /// Both p and q must be >= 1.
294
- pub fn update(r: *BigRational, p: anytype, q: anytype) !void {
295
- assert(p >= 1);
296
- assert(q >= 1);
418
+ var u = R{ .p = 3, .q = 5 };
419
+ var v = R{ .p = 2, .q = 5 };
420
+ try expectEqual(u.cmp(&v), .gt);
421
+ v = R{ .p = 4, .q = 5 };
422
+ try expectEqual(u.cmp(&v), .lt);
297
423
 
298
- var s = try std.math.big.Rational.init(r.val.p.allocator);
299
- defer s.deinit();
300
- try s.setRatio(p, q);
301
- try r.val.mul(r.val, s);
302
- }
424
+ v = R{ .p = 3, .q = 6 };
425
+ try expectEqual(u.cmp(&v), .gt);
426
+ v = R{ .p = 3, .q = 4 };
427
+ try expectEqual(u.cmp(&v), .lt);
303
428
 
304
- /// Adds two rationals.
305
- pub fn add(r: *BigRational, s: *const BigRational) !void {
306
- try r.val.add(r.val, s.val);
307
- }
429
+ v = R{ .p = 3, .q = 5 };
430
+ try expectEqual(u.cmp(&v), .eq);
308
431
 
309
- /// Multiplies two rationals.
310
- pub fn mul(r: *BigRational, s: *const BigRational) !void {
311
- try r.val.mul(r.val, s.val);
432
+ try doTurn(&u);
433
+ v = R{ .p = 4567216874, .q = 89124781931235 };
434
+ if (t == u64) {
435
+ try expectEqual(u.cmp(&v), error.Overflow);
436
+ } else {
437
+ try expectEqual(u.cmp(&v), .lt);
438
+ }
312
439
  }
313
- };
314
-
315
- test BigRational {
316
- var r = try BigRational.init(std.testing.allocator);
317
- defer r.deinit();
318
- var s = try std.math.big.Rational.init(std.testing.allocator);
319
- defer s.deinit();
320
-
321
- try r.reset();
322
-
323
- var c: u8 = 128;
324
- _ = &c;
325
-
326
- try r.update(c, 256);
327
- try doTurn(&r);
328
-
329
- try s.setRatio(75383, 35550920704);
330
- try expect((try s.order(r.val)) == .eq);
331
-
332
- try r.update(1, 4);
333
- try doTurn(&r);
334
-
335
- try s.setRatio(5682596689, 2527735925804191711232);
336
- try expect((try s.order(r.val)) == .eq);
337
-
338
- try r.reset();
339
-
340
- var t = try BigRational.init(std.testing.allocator);
341
- defer t.deinit();
342
-
343
- try t.val.setRatio(10, 13);
344
- try r.mul(&t);
345
- try t.val.setRatio(3, 4);
346
- try r.mul(&t);
347
-
348
- try s.setRatio(15, 26);
349
- try expect((try s.order(r.val)) == .eq);
350
-
351
- try t.val.setRatio(1, 3);
352
- try r.add(&t);
353
-
354
- try s.setRatio(71, 78);
355
- try expect((try s.order(r.val)) == .eq);
356
440
  }