Skip to content

Commit afa23aa

Browse files
fix: typeinfo enums (zig v0.14), add tests
1 parent 7b0ff98 commit afa23aa

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/random.zig

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ pub const Sfc32 = @import("random/sfc32.zig");
77

88
/// Returns uniformly distributed value in [-n..n) interval
99
pub fn norm(comptime T: type, rnd: *Random, n: T) T {
10-
std.debug.assert(@typeInfo(T) == .Float);
11-
return rnd.float(T) * 2 * n - n;
10+
std.debug.assert(@typeInfo(T) == .float);
11+
return rnd.float(T) * 2.0 * n - n;
1212
}
1313

1414
/// Returns uniformly distributed value in either the
1515
/// [min..max) or the (-max..-min] interval (same odds)
1616
pub fn normMinmax(comptime T: type, rnd: *Random, min: T, max: T) T {
17-
std.debug.assert(@typeInfo(T) == .Float);
17+
std.debug.assert(@typeInfo(T) == .float);
1818
const x = minmax(T, rnd, min, max);
1919
return if (rnd.int(u1) == 1) x else -x;
2020
}
2121

2222
/// Returns uniformly distributed value in [min..max) interval
2323
pub fn minmax(comptime T: type, rnd: *Random, min: T, max: T) T {
24-
std.debug.assert(@typeInfo(T) == .Float);
24+
std.debug.assert(@typeInfo(T) == .float);
2525
return min + (max - min) * rnd.float(T);
2626
}
2727

@@ -44,3 +44,12 @@ pub fn chance(rnd: *Random, prob: f32) bool {
4444
test {
4545
std.testing.refAllDecls(@This());
4646
}
47+
48+
test "norm" {
49+
var sfc = Sfc32.init(&.{ 1, 2, 3, 4 });
50+
var rnd = Sfc32.random(&sfc);
51+
try std.testing.expectApproxEqAbs(-1, norm(f32, &rnd, 1), 0.01);
52+
try std.testing.expectApproxEqAbs(-0.89, norm(f32, &rnd, 1), 0.01);
53+
try std.testing.expectApproxEqAbs(-1.06, normMinmax(f32, &rnd, 1, 2), 0.01);
54+
try std.testing.expectApproxEqAbs(-1.30, normMinmax(f32, &rnd, 1, 2), 0.01);
55+
}

0 commit comments

Comments
 (0)