@pkmn/engine 0.1.0-dev.f3aaee39 → 0.1.0-dev.f3b63b86
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.
- package/LICENSE +1 -3
- package/README.md +77 -23
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +47 -0
- package/build/pkg/addon/browser.js.map +1 -0
- package/build/pkg/addon/node.d.ts +3 -0
- package/build/pkg/addon/node.js +110 -0
- package/build/pkg/addon/node.js.map +1 -0
- package/build/pkg/addon/wasm.d.ts +2 -0
- package/build/pkg/addon/wasm.js +53 -0
- package/build/pkg/addon/wasm.js.map +1 -0
- package/build/pkg/addon.d.ts +26 -4
- package/build/pkg/addon.js +39 -71
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +17 -4
- package/build/pkg/data/protocol.json +84 -13
- package/build/pkg/data.d.ts +27 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +15 -18
- package/build/pkg/gen1.js +30 -21
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +42 -91
- package/build/pkg/index.js +23 -70
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +20 -1
- package/build/pkg/protocol.js +123 -102
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +69 -74
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +152 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +152 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +359 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +33 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +273 -120
- package/package.json +64 -123
- package/src/bin/install-pkmn-engine +47 -21
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +200 -0
- package/src/lib/{binding → bindings}/node.zig +15 -32
- package/src/lib/bindings/wasm.zig +50 -0
- package/src/lib/common/array.zig +70 -0
- package/src/lib/common/data.zig +12 -24
- package/src/lib/common/debug.zig +32 -11
- package/src/lib/common/js.zig +3 -7
- package/src/lib/common/optional.zig +19 -18
- package/src/lib/common/options.zig +6 -0
- package/src/lib/common/protocol.zig +919 -430
- package/src/lib/common/rational.zig +341 -254
- package/src/lib/common/rng.zig +22 -15
- package/src/lib/common/util.zig +33 -1
- package/src/lib/gen1/calc.zig +706 -177
- package/src/lib/gen1/chance.zig +428 -302
- package/src/lib/gen1/data/moves.zig +193 -183
- package/src/lib/gen1/data/species.zig +4 -6
- package/src/lib/gen1/data/types.zig +9 -10
- package/src/lib/gen1/data.zig +60 -75
- package/src/lib/gen1/helpers.zig +90 -563
- package/src/lib/gen1/mechanics.zig +682 -600
- package/src/lib/gen2/calc.zig +150 -0
- package/src/lib/gen2/chance.zig +896 -0
- package/src/lib/gen2/data/items.zig +13 -21
- package/src/lib/gen2/data/moves.zig +664 -572
- package/src/lib/gen2/data/species.zig +2 -4
- package/src/lib/gen2/data/types.zig +139 -34
- package/src/lib/gen2/data.zig +121 -96
- package/src/lib/gen2/generated.zig +801 -0
- package/src/lib/gen2/helpers.zig +53 -38
- package/src/lib/gen2/mechanics.zig +3580 -29
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +14 -0
- package/src/lib/pkmn.zig +125 -29
- package/src/lib/wasm.zig +13 -0
- package/src/tools/display/dom.ts +93 -0
- package/src/tools/display/ui.css +354 -0
- package/build/test/display.js +0 -400
- package/build/test/display.js.map +0 -1
- package/src/lib/binding/wasm.zig +0 -12
- package/src/lib/gen1/README.md +0 -892
- package/src/lib/gen2/README.md +0 -308
- 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
|
-
|
|
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 =
|
|
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,182 +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
|
-
|
|
47
|
-
// std.debug.print("({d}/{d}) * ", .{p, q});
|
|
48
|
-
assert(p >= 1);
|
|
49
|
-
assert(q >= 1);
|
|
50
|
-
|
|
51
|
-
// If our parameters are not fully reduced they may prematurely
|
|
52
|
-
// cause overflow/loss of precision after the multiplication below
|
|
53
|
-
assert(switch (@typeInfo(@TypeOf(p, q))) {
|
|
54
|
-
.ComptimeInt, .ComptimeFloat => comptime gcd(p, q),
|
|
55
|
-
else => 1,
|
|
56
|
-
} == 1);
|
|
57
|
-
|
|
58
|
-
switch (@typeInfo(T)) {
|
|
59
|
-
.Int => {
|
|
60
|
-
// Greedily attempt to multiply and if it fails, reduce and try again
|
|
61
|
-
r.multiplication(p, q) catch |err| switch (err) {
|
|
62
|
-
error.Overflow => {
|
|
63
|
-
r.reduce();
|
|
64
|
-
try r.multiplication(p, q);
|
|
65
|
-
},
|
|
66
|
-
else => unreachable,
|
|
67
|
-
};
|
|
68
|
-
},
|
|
69
|
-
.Float => {
|
|
70
|
-
// Reduce in situations where we're likely to start losing precision
|
|
71
|
-
if (r.q > REDUCE or r.p > REDUCE) r.reduce();
|
|
72
|
-
|
|
73
|
-
r.p *= switch (@typeInfo(@TypeOf(p))) {
|
|
74
|
-
.Float, .ComptimeFloat => p,
|
|
75
|
-
else => @floatFromInt(p),
|
|
76
|
-
};
|
|
77
|
-
r.q *= switch (@typeInfo(@TypeOf(q))) {
|
|
78
|
-
.Float, .ComptimeFloat => q,
|
|
79
|
-
else => @floatFromInt(q),
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// We should always be dealing with whole numbers
|
|
83
|
-
assert(std.math.modf(r.p).fpart == 0);
|
|
84
|
-
assert(std.math.modf(r.q).fpart == 0);
|
|
85
|
-
},
|
|
86
|
-
else => unreachable,
|
|
87
|
-
}
|
|
51
|
+
return update_(T, o, r, p, q);
|
|
88
52
|
}
|
|
89
53
|
|
|
90
54
|
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
91
|
-
pub fn add(r: *Self, s:
|
|
92
|
-
|
|
93
|
-
.Int => {
|
|
94
|
-
if (r.q == s.q) {
|
|
95
|
-
r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
|
|
96
|
-
error.Overflow => val: {
|
|
97
|
-
r.reduce();
|
|
98
|
-
s.reduce();
|
|
99
|
-
break :val try std.math.add(T, r.p, s.p);
|
|
100
|
-
},
|
|
101
|
-
else => unreachable,
|
|
102
|
-
};
|
|
103
|
-
} else {
|
|
104
|
-
r.addition(s.p, s.q) catch |err| switch (err) {
|
|
105
|
-
error.Overflow => {
|
|
106
|
-
r.reduce();
|
|
107
|
-
s.reduce();
|
|
108
|
-
try r.addition(s.p, s.q);
|
|
109
|
-
},
|
|
110
|
-
else => unreachable,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
.Float => {
|
|
115
|
-
if (r.q == s.q) {
|
|
116
|
-
if (r.p > REDUCE) r.reduce();
|
|
117
|
-
if (s.p > REDUCE) s.reduce();
|
|
118
|
-
|
|
119
|
-
r.p += s.p;
|
|
120
|
-
} else {
|
|
121
|
-
// Always reduce to minimize loss of precision from the multiplications
|
|
122
|
-
r.reduce();
|
|
123
|
-
s.reduce();
|
|
124
|
-
|
|
125
|
-
r.p = (r.p * s.q) + (r.q * s.p);
|
|
126
|
-
r.q *= s.q;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
assert(std.math.modf(r.p).fpart == 0);
|
|
130
|
-
assert(std.math.modf(r.q).fpart == 0);
|
|
131
|
-
},
|
|
132
|
-
else => unreachable,
|
|
133
|
-
}
|
|
55
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
56
|
+
return add_(T, o, r, s);
|
|
134
57
|
}
|
|
135
58
|
|
|
136
59
|
/// Multiplies two rationals.
|
|
137
|
-
pub fn mul(r: *Self, s:
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
r.multiplication(s.p, s.q) catch |err| switch (err) {
|
|
141
|
-
error.Overflow => {
|
|
142
|
-
r.reduce();
|
|
143
|
-
s.reduce();
|
|
144
|
-
try r.multiplication(s.p, s.q);
|
|
145
|
-
},
|
|
146
|
-
else => unreachable,
|
|
147
|
-
};
|
|
148
|
-
},
|
|
149
|
-
.Float => {
|
|
150
|
-
if (r.q > REDUCE or r.p > REDUCE) r.reduce();
|
|
151
|
-
if (s.q > REDUCE or s.p > REDUCE) s.reduce();
|
|
152
|
-
|
|
153
|
-
r.p *= s.p;
|
|
154
|
-
r.q *= s.q;
|
|
60
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
61
|
+
return mul_(T, o, r, s);
|
|
62
|
+
}
|
|
155
63
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
else => unreachable,
|
|
160
|
-
}
|
|
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);
|
|
161
67
|
}
|
|
162
68
|
|
|
163
69
|
/// Normalize the rational by reducing by the greatest common divisor.
|
|
164
70
|
pub fn reduce(r: *Self) void {
|
|
165
|
-
|
|
166
|
-
|
|
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();
|
|
167
79
|
|
|
168
|
-
|
|
169
|
-
|
|
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,
|
|
170
84
|
|
|
171
|
-
|
|
172
|
-
|
|
85
|
+
/// Possible error returned by operations on the Rational.
|
|
86
|
+
pub const Error = Err;
|
|
173
87
|
|
|
174
|
-
|
|
175
|
-
|
|
88
|
+
/// Resets the rational back to 1.
|
|
89
|
+
pub fn reset(r: *Self) void {
|
|
90
|
+
r.p = 1;
|
|
91
|
+
r.q = 1;
|
|
176
92
|
}
|
|
177
93
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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);
|
|
186
108
|
}
|
|
187
109
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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);
|
|
191
113
|
}
|
|
192
114
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
var 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
|
+
}
|
|
198
119
|
|
|
199
|
-
|
|
200
|
-
|
|
120
|
+
pub fn format(self: Self, writer: *std.Io.Writer) !void {
|
|
121
|
+
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
201
122
|
}
|
|
202
123
|
};
|
|
203
124
|
}
|
|
204
125
|
|
|
205
|
-
|
|
206
|
-
|
|
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
|
+
|
|
305
|
+
fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
207
306
|
assert(p >= 1);
|
|
208
307
|
assert(q >= 1);
|
|
209
308
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
+
}
|
|
213
349
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
a = c;
|
|
350
|
+
assert(a > 0);
|
|
351
|
+
return a;
|
|
352
|
+
},
|
|
218
353
|
}
|
|
354
|
+
}
|
|
219
355
|
|
|
220
|
-
|
|
221
|
-
|
|
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
|
|
222
372
|
}
|
|
223
373
|
|
|
224
374
|
fn doTurn(r: anytype) !void {
|
|
@@ -230,15 +380,18 @@ fn doTurn(r: anytype) !void {
|
|
|
230
380
|
}
|
|
231
381
|
|
|
232
382
|
test Rational {
|
|
233
|
-
inline for (.{ u64, u128, f64 }) |t| {
|
|
234
|
-
|
|
383
|
+
inline for (.{ u64, u128, u256, f64 }) |t| {
|
|
384
|
+
const R = Rational(t);
|
|
385
|
+
var r: R = .{};
|
|
235
386
|
|
|
236
387
|
var c: t = 128;
|
|
388
|
+
_ = &c;
|
|
389
|
+
|
|
237
390
|
try r.update(c, 256);
|
|
238
391
|
try doTurn(&r);
|
|
239
392
|
|
|
240
393
|
r.reduce();
|
|
241
|
-
try expectEqual(
|
|
394
|
+
try expectEqual(R{ .p = 75383, .q = 35550920704 }, r);
|
|
242
395
|
|
|
243
396
|
try r.update(1, 4);
|
|
244
397
|
if (t == u64) {
|
|
@@ -246,108 +399,42 @@ test Rational {
|
|
|
246
399
|
} else {
|
|
247
400
|
try doTurn(&r);
|
|
248
401
|
r.reduce();
|
|
249
|
-
try expectEqual(
|
|
402
|
+
try expectEqual(R{ .p = 5682596689, .q = 2527735925804191711232 }, r);
|
|
250
403
|
}
|
|
251
404
|
|
|
252
405
|
r.reset();
|
|
253
406
|
|
|
254
|
-
var s =
|
|
407
|
+
var s = R{ .p = 10, .q = 13 };
|
|
255
408
|
try r.mul(&s);
|
|
256
|
-
s =
|
|
409
|
+
s = R{ .p = 3, .q = 4 };
|
|
257
410
|
try r.mul(&s);
|
|
258
|
-
try expectEqual(
|
|
411
|
+
try expectEqual(R{ .p = 30, .q = 52 }, r);
|
|
259
412
|
|
|
260
|
-
s =
|
|
413
|
+
s = R{ .p = 1, .q = 3 };
|
|
261
414
|
try r.add(&s);
|
|
262
415
|
r.reduce();
|
|
263
|
-
try expectEqual(
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/// Adapter of std.math.big.Rational to the engines Rational interface.
|
|
268
|
-
pub const BigRational = struct {
|
|
269
|
-
/// The underlying rational number.
|
|
270
|
-
val: std.math.big.Rational,
|
|
416
|
+
try expectEqual(R{ .p = 71, .q = 78 }, r);
|
|
271
417
|
|
|
272
|
-
|
|
273
|
-
|
|
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);
|
|
274
423
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/// Frees all memory associated with a rational.
|
|
282
|
-
pub fn deinit(r: *BigRational) void {
|
|
283
|
-
r.val.deinit();
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/// Resets the rational back to 1.
|
|
287
|
-
pub fn reset(r: *BigRational) !void {
|
|
288
|
-
try r.val.setInt(1);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
292
|
-
/// Both p and q must be >= 1.
|
|
293
|
-
pub fn update(r: *BigRational, p: anytype, q: anytype) !void {
|
|
294
|
-
assert(p >= 1);
|
|
295
|
-
assert(q >= 1);
|
|
296
|
-
|
|
297
|
-
var s = try std.math.big.Rational.init(r.val.p.allocator);
|
|
298
|
-
defer s.deinit();
|
|
299
|
-
try s.setRatio(p, q);
|
|
300
|
-
try r.val.mul(r.val, s);
|
|
301
|
-
}
|
|
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);
|
|
302
428
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
try r.val.add(r.val, s.val);
|
|
306
|
-
}
|
|
429
|
+
v = R{ .p = 3, .q = 5 };
|
|
430
|
+
try expectEqual(u.cmp(&v), .eq);
|
|
307
431
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
+
}
|
|
311
439
|
}
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
test BigRational {
|
|
315
|
-
var r = try BigRational.init(std.testing.allocator);
|
|
316
|
-
defer r.deinit();
|
|
317
|
-
var s = try std.math.big.Rational.init(std.testing.allocator);
|
|
318
|
-
defer s.deinit();
|
|
319
|
-
|
|
320
|
-
try r.reset();
|
|
321
|
-
|
|
322
|
-
var c: u8 = 128;
|
|
323
|
-
try r.update(c, 256);
|
|
324
|
-
try doTurn(&r);
|
|
325
|
-
|
|
326
|
-
try s.setRatio(75383, 35550920704);
|
|
327
|
-
try expect((try s.order(r.val)) == .eq);
|
|
328
|
-
|
|
329
|
-
try r.update(1, 4);
|
|
330
|
-
try doTurn(&r);
|
|
331
|
-
|
|
332
|
-
try s.setRatio(5682596689, 2527735925804191711232);
|
|
333
|
-
try expect((try s.order(r.val)) == .eq);
|
|
334
|
-
|
|
335
|
-
try r.reset();
|
|
336
|
-
|
|
337
|
-
var t = try BigRational.init(std.testing.allocator);
|
|
338
|
-
defer t.deinit();
|
|
339
|
-
|
|
340
|
-
try t.val.setRatio(10, 13);
|
|
341
|
-
try r.mul(&t);
|
|
342
|
-
try t.val.setRatio(3, 4);
|
|
343
|
-
try r.mul(&t);
|
|
344
|
-
|
|
345
|
-
try s.setRatio(15, 26);
|
|
346
|
-
try expect((try s.order(r.val)) == .eq);
|
|
347
|
-
|
|
348
|
-
try t.val.setRatio(1, 3);
|
|
349
|
-
try r.add(&t);
|
|
350
|
-
|
|
351
|
-
try s.setRatio(71, 78);
|
|
352
|
-
try expect((try s.order(r.val)) == .eq);
|
|
353
440
|
}
|