@pkmn/engine 0.1.0-dev.7963b917 → 0.1.0-dev.7c1336ac
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 +104 -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 +25 -3
- 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 +53 -76
- 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 +368 -109
- package/package.json +62 -123
- package/src/bin/install-pkmn-engine +32 -21
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +255 -0
- package/src/lib/{binding → bindings}/node.zig +9 -22
- package/src/lib/bindings/wasm.zig +59 -0
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/data.zig +12 -24
- package/src/lib/common/debug.zig +39 -8
- package/src/lib/common/js.zig +4 -2
- package/src/lib/common/optional.zig +21 -10
- package/src/lib/common/options.zig +6 -0
- package/src/lib/common/protocol.zig +808 -332
- package/src/lib/common/rational.zig +412 -168
- package/src/lib/common/rng.zig +22 -15
- package/src/lib/common/util.zig +65 -1
- package/src/lib/gen1/calc.zig +700 -165
- package/src/lib/gen1/chance.zig +437 -296
- 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 +8 -9
- package/src/lib/gen1/data.zig +55 -70
- package/src/lib/gen1/helpers.zig +91 -409
- package/src/lib/gen1/mechanics.zig +669 -589
- package/src/lib/gen2/calc.zig +153 -0
- package/src/lib/gen2/chance.zig +902 -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 +135 -33
- package/src/lib/gen2/data.zig +115 -91
- package/src/lib/gen2/generated.zig +801 -0
- package/src/lib/gen2/helpers.zig +54 -37
- package/src/lib/gen2/mechanics.zig +3580 -29
- package/src/lib/node.zig +18 -0
- package/src/lib/pkmn.zig +37 -15
- package/src/lib/wasm.zig +17 -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 -812
- package/src/lib/gen2/README.md +0 -308
- package/src/test/display/pkmn.html.tmpl +0 -368
|
@@ -6,11 +6,36 @@ const expect = std.testing.expect;
|
|
|
6
6
|
const expectEqual = std.testing.expectEqual;
|
|
7
7
|
const expectError = std.testing.expectError;
|
|
8
8
|
|
|
9
|
+
const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
|
|
10
|
+
const Float = if (@hasField(std.builtin.Type, "float")) .float else .Float;
|
|
11
|
+
const ComptimeInt =
|
|
12
|
+
if (@hasField(std.builtin.Type, "comptime_int")) .comptime_int else .ComptimeInt;
|
|
13
|
+
const ComptimeFloat =
|
|
14
|
+
if (@hasField(std.builtin.Type, "comptime_float")) .comptime_float else .ComptimeFloat;
|
|
15
|
+
|
|
9
16
|
/// Specialization of a rational number used by the engine to compute probabilties.
|
|
10
17
|
/// For performance reasons the rational is only reduced lazily and thus `reduce` must be
|
|
11
|
-
/// invoked explicitly before reading.
|
|
18
|
+
/// invoked explicitly before reading. Note that this laziness means that this implementation
|
|
19
|
+
/// sometimes overflows in places where an eager reduced implementation would not - to achieve
|
|
20
|
+
/// the same behavior as an eager implementation simply call `reduce` after each operation.
|
|
12
21
|
pub fn Rational(comptime T: type) type {
|
|
13
|
-
|
|
22
|
+
// With floats we can't rely on an overflow bit to let us know when to reduce, so we
|
|
23
|
+
// instead start reducing when we get sufficiently close to the limit of the mantissa
|
|
24
|
+
// (in our domain we expect updates to involve numbers < 2**10, so we should be safe
|
|
25
|
+
// not reducing before we are 2**10 away from "overflowing" the mantissa)
|
|
26
|
+
const o = if (@typeInfo(T) == Float)
|
|
27
|
+
std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
|
|
28
|
+
else
|
|
29
|
+
0;
|
|
30
|
+
|
|
31
|
+
const Err = switch (@typeInfo(T)) {
|
|
32
|
+
Int => error{Overflow},
|
|
33
|
+
Float => error{},
|
|
34
|
+
else => unreachable,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Zig doesn't allow integers > u128 in an extern struct...
|
|
38
|
+
return if (@sizeOf(T) <= 16) extern struct {
|
|
14
39
|
const Self = @This();
|
|
15
40
|
|
|
16
41
|
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
@@ -18,21 +43,8 @@ pub fn Rational(comptime T: type) type {
|
|
|
18
43
|
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
19
44
|
q: T = 1,
|
|
20
45
|
|
|
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
46
|
/// Possible error returned by operations on the Rational.
|
|
31
|
-
pub const Error =
|
|
32
|
-
.Int => error{Overflow},
|
|
33
|
-
.Float => error{},
|
|
34
|
-
else => unreachable,
|
|
35
|
-
};
|
|
47
|
+
pub const Error = Err;
|
|
36
48
|
|
|
37
49
|
/// Resets the rational back to 1.
|
|
38
50
|
pub fn reset(r: *Self) void {
|
|
@@ -43,136 +55,27 @@ pub fn Rational(comptime T: type) type {
|
|
|
43
55
|
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
44
56
|
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
45
57
|
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
|
-
}
|
|
58
|
+
return update_(T, o, r, p, q);
|
|
88
59
|
}
|
|
89
60
|
|
|
90
61
|
/// 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
|
-
}
|
|
62
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
63
|
+
return add_(T, o, r, s);
|
|
134
64
|
}
|
|
135
65
|
|
|
136
66
|
/// 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;
|
|
67
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
68
|
+
return mul_(T, o, r, s);
|
|
69
|
+
}
|
|
155
70
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
else => unreachable,
|
|
160
|
-
}
|
|
71
|
+
/// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
|
|
72
|
+
pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
|
|
73
|
+
return cmp_(T, o, r, s);
|
|
161
74
|
}
|
|
162
75
|
|
|
163
76
|
/// Normalize the rational by reducing by the greatest common divisor.
|
|
164
77
|
pub fn reduce(r: *Self) void {
|
|
165
|
-
|
|
166
|
-
if (d == 1) return;
|
|
167
|
-
|
|
168
|
-
assert(@mod(r.p, d) == 0);
|
|
169
|
-
assert(@mod(r.q, d) == 0);
|
|
170
|
-
|
|
171
|
-
r.p /= d;
|
|
172
|
-
r.q /= d;
|
|
173
|
-
|
|
174
|
-
assert(r.p >= 1);
|
|
175
|
-
assert(r.q >= 1);
|
|
78
|
+
reduce_(r);
|
|
176
79
|
}
|
|
177
80
|
|
|
178
81
|
pub fn format(
|
|
@@ -184,41 +87,308 @@ pub fn Rational(comptime T: type) type {
|
|
|
184
87
|
_ = .{ fmt, opts };
|
|
185
88
|
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
186
89
|
}
|
|
90
|
+
} else struct {
|
|
91
|
+
const Self = @This();
|
|
92
|
+
|
|
93
|
+
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
94
|
+
p: T = 1,
|
|
95
|
+
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
96
|
+
q: T = 1,
|
|
187
97
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
98
|
+
/// Possible error returned by operations on the Rational.
|
|
99
|
+
pub const Error = Err;
|
|
100
|
+
|
|
101
|
+
/// Resets the rational back to 1.
|
|
102
|
+
pub fn reset(r: *Self) void {
|
|
103
|
+
r.p = 1;
|
|
104
|
+
r.q = 1;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
108
|
+
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
109
|
+
pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
|
|
110
|
+
return update_(T, o, r, p, q);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
114
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
115
|
+
return add_(T, o, r, s);
|
|
191
116
|
}
|
|
192
117
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
var n2 = try std.math.mul(T, r.q, p);
|
|
118
|
+
/// Multiplies two rationals.
|
|
119
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
120
|
+
return mul_(T, o, r, s);
|
|
121
|
+
}
|
|
198
122
|
|
|
199
|
-
|
|
200
|
-
|
|
123
|
+
/// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
|
|
124
|
+
pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
|
|
125
|
+
return cmp_(T, o, r, s);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// Normalize the rational by reducing by the greatest common divisor.
|
|
129
|
+
pub fn reduce(r: *Self) void {
|
|
130
|
+
reduce_(r);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
pub fn format(
|
|
134
|
+
self: Self,
|
|
135
|
+
comptime fmt: []const u8,
|
|
136
|
+
opts: std.fmt.FormatOptions,
|
|
137
|
+
writer: anytype,
|
|
138
|
+
) !void {
|
|
139
|
+
_ = .{ fmt, opts };
|
|
140
|
+
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
201
141
|
}
|
|
202
142
|
};
|
|
203
143
|
}
|
|
204
144
|
|
|
205
|
-
|
|
206
|
-
|
|
145
|
+
fn update_(comptime T: type, comptime o: comptime_int, r: anytype, p: anytype, q: anytype) !void {
|
|
146
|
+
// std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
|
|
147
|
+
assert(p >= 1);
|
|
148
|
+
assert(q >= 1);
|
|
149
|
+
|
|
150
|
+
// If our parameters are not fully reduced they may prematurely
|
|
151
|
+
// cause overflow/loss of precision after the multiplication below
|
|
152
|
+
assert(switch (@typeInfo(@TypeOf(p, q))) {
|
|
153
|
+
ComptimeInt, ComptimeFloat => comptime gcd(p, q),
|
|
154
|
+
else => 1,
|
|
155
|
+
} == 1);
|
|
156
|
+
|
|
157
|
+
switch (@typeInfo(T)) {
|
|
158
|
+
Int => {
|
|
159
|
+
// Greedily attempt to multiply and if it fails, reduce and try again
|
|
160
|
+
multiplication(T, r, p, q) catch |err| switch (err) {
|
|
161
|
+
error.Overflow => {
|
|
162
|
+
r.reduce();
|
|
163
|
+
try multiplication(T, r, p, q);
|
|
164
|
+
},
|
|
165
|
+
else => unreachable,
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
Float => {
|
|
169
|
+
// Reduce in situations where we're likely to start losing precision
|
|
170
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
171
|
+
|
|
172
|
+
r.p *= switch (@typeInfo(@TypeOf(p))) {
|
|
173
|
+
Float, ComptimeFloat => p,
|
|
174
|
+
else => @floatFromInt(p),
|
|
175
|
+
};
|
|
176
|
+
r.q *= switch (@typeInfo(@TypeOf(q))) {
|
|
177
|
+
Float, ComptimeFloat => q,
|
|
178
|
+
else => @floatFromInt(q),
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// We should always be dealing with whole numbers
|
|
182
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
183
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
184
|
+
},
|
|
185
|
+
else => unreachable,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn add_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
|
|
190
|
+
switch (@typeInfo(T)) {
|
|
191
|
+
Int => {
|
|
192
|
+
if (r.q == s.q) {
|
|
193
|
+
r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
|
|
194
|
+
error.Overflow => val: {
|
|
195
|
+
r.reduce();
|
|
196
|
+
s.reduce();
|
|
197
|
+
break :val try std.math.add(T, r.p, s.p);
|
|
198
|
+
},
|
|
199
|
+
else => unreachable,
|
|
200
|
+
};
|
|
201
|
+
} else {
|
|
202
|
+
addition(T, r, s.p, s.q) catch |err| switch (err) {
|
|
203
|
+
error.Overflow => {
|
|
204
|
+
r.reduce();
|
|
205
|
+
s.reduce();
|
|
206
|
+
try addition(T, r, s.p, s.q);
|
|
207
|
+
},
|
|
208
|
+
else => unreachable,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
Float => {
|
|
213
|
+
if (r.q == s.q) {
|
|
214
|
+
if (r.p > o) r.reduce();
|
|
215
|
+
if (s.p > o) s.reduce();
|
|
216
|
+
|
|
217
|
+
r.p += s.p;
|
|
218
|
+
} else {
|
|
219
|
+
// Always reduce to minimize loss of precision from the multiplications
|
|
220
|
+
r.reduce();
|
|
221
|
+
s.reduce();
|
|
222
|
+
|
|
223
|
+
r.p = (r.p * s.q) + (r.q * s.p);
|
|
224
|
+
r.q *= s.q;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
228
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
229
|
+
},
|
|
230
|
+
else => unreachable,
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn mul_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
|
|
235
|
+
switch (@typeInfo(T)) {
|
|
236
|
+
Int => {
|
|
237
|
+
multiplication(T, r, s.p, s.q) catch |err| switch (err) {
|
|
238
|
+
error.Overflow => {
|
|
239
|
+
r.reduce();
|
|
240
|
+
s.reduce();
|
|
241
|
+
try multiplication(T, r, s.p, s.q);
|
|
242
|
+
},
|
|
243
|
+
else => unreachable,
|
|
244
|
+
};
|
|
245
|
+
},
|
|
246
|
+
Float => {
|
|
247
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
248
|
+
if (s.q > o or s.p > o) s.reduce();
|
|
249
|
+
|
|
250
|
+
r.p *= s.p;
|
|
251
|
+
r.q *= s.q;
|
|
252
|
+
|
|
253
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
254
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
255
|
+
},
|
|
256
|
+
else => unreachable,
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn cmp_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !std.math.Order {
|
|
261
|
+
switch (@typeInfo(T)) {
|
|
262
|
+
Int => {
|
|
263
|
+
const ad = std.math.mul(T, r.p, s.q) catch |err| switch (err) {
|
|
264
|
+
error.Overflow => ad: {
|
|
265
|
+
r.reduce();
|
|
266
|
+
s.reduce();
|
|
267
|
+
break :ad try std.math.mul(T, r.p, s.q);
|
|
268
|
+
},
|
|
269
|
+
else => unreachable,
|
|
270
|
+
};
|
|
271
|
+
const bc = std.math.mul(T, r.q, s.p) catch |err| switch (err) {
|
|
272
|
+
error.Overflow => bc: {
|
|
273
|
+
r.reduce();
|
|
274
|
+
s.reduce();
|
|
275
|
+
break :bc try std.math.mul(T, r.q, s.p);
|
|
276
|
+
},
|
|
277
|
+
else => unreachable,
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
return std.math.order(ad, bc);
|
|
281
|
+
},
|
|
282
|
+
Float => {
|
|
283
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
284
|
+
if (s.q > o or s.p > o) s.reduce();
|
|
285
|
+
|
|
286
|
+
const ad: T = r.p * s.q;
|
|
287
|
+
const bc: T = r.q * s.p;
|
|
288
|
+
|
|
289
|
+
return std.math.order(ad, bc);
|
|
290
|
+
},
|
|
291
|
+
else => unreachable,
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
fn reduce_(r: anytype) void {
|
|
296
|
+
const d = gcd(r.p, r.q);
|
|
297
|
+
if (d == 1) return;
|
|
298
|
+
|
|
299
|
+
assert(@mod(r.p, d) == 0);
|
|
300
|
+
assert(@mod(r.q, d) == 0);
|
|
301
|
+
|
|
302
|
+
r.p /= d;
|
|
303
|
+
r.q /= d;
|
|
304
|
+
|
|
305
|
+
assert(r.p >= 1);
|
|
306
|
+
assert(r.q >= 1);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
fn multiplication(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
|
|
310
|
+
r.p = try std.math.mul(T, r.p, p);
|
|
311
|
+
r.q = try std.math.mul(T, r.q, q);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
fn addition(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
|
|
315
|
+
// (a/b) + (c/d) = (ad+bc)/(bd)
|
|
316
|
+
const bd = try std.math.mul(T, r.q, q);
|
|
317
|
+
const ad = try std.math.mul(T, r.p, q);
|
|
318
|
+
const bc = try std.math.mul(T, r.q, p);
|
|
319
|
+
|
|
320
|
+
r.p = try std.math.add(T, ad, bc);
|
|
321
|
+
r.q = bd;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
207
325
|
assert(p >= 1);
|
|
208
326
|
assert(q >= 1);
|
|
209
327
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
328
|
+
// convert comptime_int to a sized integer within this function so that @ctz will work
|
|
329
|
+
const T = switch (@TypeOf(p, q)) {
|
|
330
|
+
comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
|
|
331
|
+
else => |U| U,
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
switch (@typeInfo(T)) {
|
|
335
|
+
Int => {
|
|
336
|
+
// std.math.gcd but without some checks because we have a stricter range
|
|
337
|
+
var x: T = @intCast(p);
|
|
338
|
+
var y: T = @intCast(q);
|
|
339
|
+
|
|
340
|
+
const xz = @ctz(x);
|
|
341
|
+
const yz = @ctz(y);
|
|
342
|
+
const shift = @min(xz, yz);
|
|
343
|
+
x >>= @intCast(xz);
|
|
344
|
+
y >>= @intCast(yz);
|
|
345
|
+
|
|
346
|
+
var diff = y -% x;
|
|
347
|
+
while (diff != 0) : (diff = y -% x) {
|
|
348
|
+
const zeros = @ctz(diff);
|
|
349
|
+
if (x > y) diff = -%diff;
|
|
350
|
+
y = @min(x, y);
|
|
351
|
+
x = diff >> @intCast(zeros);
|
|
352
|
+
}
|
|
213
353
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
354
|
+
const result = y << @intCast(shift);
|
|
355
|
+
assert(result > 0);
|
|
356
|
+
return result;
|
|
357
|
+
},
|
|
358
|
+
else => {
|
|
359
|
+
var a = p;
|
|
360
|
+
var b = q;
|
|
361
|
+
var c: T = undefined;
|
|
362
|
+
|
|
363
|
+
while (b != 0) {
|
|
364
|
+
c = b;
|
|
365
|
+
b = @mod(a, b);
|
|
366
|
+
a = c;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
assert(a > 0);
|
|
370
|
+
return a;
|
|
371
|
+
},
|
|
218
372
|
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
test gcd {
|
|
376
|
+
const seed = if (@hasDecl(std.testing, "random_seed")) std.testing.random_seed else 0x12345678;
|
|
377
|
+
var prng = (if (@hasDecl(std, "Random")) std.Random else std.rand).DefaultPrng.init(seed);
|
|
378
|
+
var random = prng.random();
|
|
379
|
+
|
|
380
|
+
for (0..1000) |_| {
|
|
381
|
+
const a = random.int(u32);
|
|
382
|
+
const b = random.int(u32);
|
|
383
|
+
if (a == 0 or b == 0) continue;
|
|
219
384
|
|
|
220
|
-
|
|
221
|
-
|
|
385
|
+
try expectEqual(
|
|
386
|
+
gcd(a, b),
|
|
387
|
+
@as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
|
|
222
392
|
}
|
|
223
393
|
|
|
224
394
|
fn doTurn(r: anytype) !void {
|
|
@@ -230,15 +400,18 @@ fn doTurn(r: anytype) !void {
|
|
|
230
400
|
}
|
|
231
401
|
|
|
232
402
|
test Rational {
|
|
233
|
-
inline for (.{ u64, u128, f64 }) |t| {
|
|
234
|
-
|
|
403
|
+
inline for (.{ u64, u128, u256, f64 }) |t| {
|
|
404
|
+
const R = Rational(t);
|
|
405
|
+
var r: R = .{};
|
|
235
406
|
|
|
236
407
|
var c: t = 128;
|
|
408
|
+
_ = &c;
|
|
409
|
+
|
|
237
410
|
try r.update(c, 256);
|
|
238
411
|
try doTurn(&r);
|
|
239
412
|
|
|
240
413
|
r.reduce();
|
|
241
|
-
try expectEqual(
|
|
414
|
+
try expectEqual(R{ .p = 75383, .q = 35550920704 }, r);
|
|
242
415
|
|
|
243
416
|
try r.update(1, 4);
|
|
244
417
|
if (t == u64) {
|
|
@@ -246,21 +419,43 @@ test Rational {
|
|
|
246
419
|
} else {
|
|
247
420
|
try doTurn(&r);
|
|
248
421
|
r.reduce();
|
|
249
|
-
try expectEqual(
|
|
422
|
+
try expectEqual(R{ .p = 5682596689, .q = 2527735925804191711232 }, r);
|
|
250
423
|
}
|
|
251
424
|
|
|
252
425
|
r.reset();
|
|
253
426
|
|
|
254
|
-
var s =
|
|
427
|
+
var s = R{ .p = 10, .q = 13 };
|
|
255
428
|
try r.mul(&s);
|
|
256
|
-
s =
|
|
429
|
+
s = R{ .p = 3, .q = 4 };
|
|
257
430
|
try r.mul(&s);
|
|
258
|
-
try expectEqual(
|
|
431
|
+
try expectEqual(R{ .p = 30, .q = 52 }, r);
|
|
259
432
|
|
|
260
|
-
s =
|
|
433
|
+
s = R{ .p = 1, .q = 3 };
|
|
261
434
|
try r.add(&s);
|
|
262
435
|
r.reduce();
|
|
263
|
-
try expectEqual(
|
|
436
|
+
try expectEqual(R{ .p = 71, .q = 78 }, r);
|
|
437
|
+
|
|
438
|
+
var u = R{ .p = 3, .q = 5 };
|
|
439
|
+
var v = R{ .p = 2, .q = 5 };
|
|
440
|
+
try expectEqual(u.cmp(&v), .gt);
|
|
441
|
+
v = R{ .p = 4, .q = 5 };
|
|
442
|
+
try expectEqual(u.cmp(&v), .lt);
|
|
443
|
+
|
|
444
|
+
v = R{ .p = 3, .q = 6 };
|
|
445
|
+
try expectEqual(u.cmp(&v), .gt);
|
|
446
|
+
v = R{ .p = 3, .q = 4 };
|
|
447
|
+
try expectEqual(u.cmp(&v), .lt);
|
|
448
|
+
|
|
449
|
+
v = R{ .p = 3, .q = 5 };
|
|
450
|
+
try expectEqual(u.cmp(&v), .eq);
|
|
451
|
+
|
|
452
|
+
try doTurn(&u);
|
|
453
|
+
v = R{ .p = 4567216874, .q = 89124781931235 };
|
|
454
|
+
if (t == u64) {
|
|
455
|
+
try expectEqual(u.cmp(&v), error.Overflow);
|
|
456
|
+
} else {
|
|
457
|
+
try expectEqual(u.cmp(&v), .lt);
|
|
458
|
+
}
|
|
264
459
|
}
|
|
265
460
|
}
|
|
266
461
|
|
|
@@ -275,7 +470,7 @@ pub const BigRational = struct {
|
|
|
275
470
|
/// Create a new BigRational wrapper. A small amount of memory will be allocated on
|
|
276
471
|
/// initialization. Not default initialized to 1 - you must explicitly `reset` first.
|
|
277
472
|
pub fn init(alloc: std.mem.Allocator) !BigRational {
|
|
278
|
-
return
|
|
473
|
+
return .{ .val = try std.math.big.Rational.init(alloc) };
|
|
279
474
|
}
|
|
280
475
|
|
|
281
476
|
/// Frees all memory associated with a rational.
|
|
@@ -301,14 +496,25 @@ pub const BigRational = struct {
|
|
|
301
496
|
}
|
|
302
497
|
|
|
303
498
|
/// Adds two rationals.
|
|
304
|
-
pub fn add(r: *BigRational, s:
|
|
305
|
-
|
|
499
|
+
pub fn add(r: *BigRational, s: anytype) !void {
|
|
500
|
+
if (@hasField(@TypeOf(s.*), "val")) {
|
|
501
|
+
try r.val.add(r.val, s.val);
|
|
502
|
+
} else {
|
|
503
|
+
var t = try std.math.big.Rational.init(r.val.p.allocator);
|
|
504
|
+
defer t.deinit();
|
|
505
|
+
try t.setRatio(s.p, s.q);
|
|
506
|
+
try r.val.add(r.val, t);
|
|
507
|
+
}
|
|
306
508
|
}
|
|
307
509
|
|
|
308
510
|
/// Multiplies two rationals.
|
|
309
511
|
pub fn mul(r: *BigRational, s: *const BigRational) !void {
|
|
310
512
|
try r.val.mul(r.val, s.val);
|
|
311
513
|
}
|
|
514
|
+
|
|
515
|
+
pub fn cmp(r: *BigRational, s: *const BigRational) Error!std.math.Order {
|
|
516
|
+
return std.math.big.Rational.order(r.val, s.val);
|
|
517
|
+
}
|
|
312
518
|
};
|
|
313
519
|
|
|
314
520
|
test BigRational {
|
|
@@ -320,6 +526,8 @@ test BigRational {
|
|
|
320
526
|
try r.reset();
|
|
321
527
|
|
|
322
528
|
var c: u8 = 128;
|
|
529
|
+
_ = &c;
|
|
530
|
+
|
|
323
531
|
try r.update(c, 256);
|
|
324
532
|
try doTurn(&r);
|
|
325
533
|
|
|
@@ -350,4 +558,40 @@ test BigRational {
|
|
|
350
558
|
|
|
351
559
|
try s.setRatio(71, 78);
|
|
352
560
|
try expect((try s.order(r.val)) == .eq);
|
|
561
|
+
|
|
562
|
+
try expectEqual(t.cmp(&r), .lt);
|
|
563
|
+
try expectEqual(r.cmp(&t), .gt);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
test "minimum" {
|
|
567
|
+
// TODO: ziglang/zig#21069
|
|
568
|
+
if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
|
|
569
|
+
return error.SkipZigTest;
|
|
570
|
+
}
|
|
571
|
+
var r = try BigRational.init(std.testing.allocator);
|
|
572
|
+
defer r.deinit();
|
|
573
|
+
var s = try std.math.big.Rational.init(std.testing.allocator);
|
|
574
|
+
defer s.deinit();
|
|
575
|
+
|
|
576
|
+
try r.reset();
|
|
577
|
+
|
|
578
|
+
try r.update(1, 2); // Speed Tie
|
|
579
|
+
for (0..2) |_| {
|
|
580
|
+
try r.update(1, 2); // Confusion
|
|
581
|
+
try r.update(1, 2); // Infatuation
|
|
582
|
+
try r.update(3, 4); // Paralysis
|
|
583
|
+
// Population Bomb
|
|
584
|
+
for (0..10) |_| {
|
|
585
|
+
try r.update(9, 10); // Accuracy
|
|
586
|
+
try r.update(1, 15); // Damage roll
|
|
587
|
+
try r.update(1, 24); // Critical Hit
|
|
588
|
+
try r.update(9, 10); // King's Rock non-proc
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const ZEROS = std.math.pow(u310, 10, 31);
|
|
593
|
+
const n = 36472996377170786403;
|
|
594
|
+
const d: u310 = 146267071761884981524886149560653378220687632500762654519853056 * ZEROS;
|
|
595
|
+
try s.setRatio(n, d);
|
|
596
|
+
try expect((try s.order(r.val)) == .eq);
|
|
353
597
|
}
|