Skip to content

Commit 64e3a75

Browse files
committed
Add comptime_float support to std.testing.approxEq(Abs/Rel)
1 parent e522b3d commit 64e3a75

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

lib/std/testing.zig

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,16 @@ pub inline fn expectApproxEqAbs(expected: anytype, actual: anytype, tolerance: a
290290

291291
fn expectApproxEqAbsInner(comptime T: type, expected: T, actual: T, tolerance: T) !void {
292292
switch (@typeInfo(T)) {
293-
.float => if (!math.approxEqAbs(T, expected, actual, tolerance)) {
293+
.float, .comptime_float => if (!math.approxEqAbs(T, expected, actual, tolerance)) {
294294
print("actual {}, not within absolute tolerance {} of expected {}\n", .{ actual, tolerance, expected });
295295
return error.TestExpectedApproxEqAbs;
296296
},
297-
298-
.comptime_float => @compileError("Cannot approximately compare two comptime_float values"),
299-
300297
else => @compileError("Unable to compare non floating point values"),
301298
}
302299
}
303300

304301
test expectApproxEqAbs {
305-
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
302+
inline for ([_]type{ f16, f32, f64, f128, comptime_float }) |T| {
306303
const pos_x: T = 12.0;
307304
const pos_y: T = 12.06;
308305
const neg_x: T = -12.0;
@@ -326,19 +323,16 @@ pub inline fn expectApproxEqRel(expected: anytype, actual: anytype, tolerance: a
326323

327324
fn expectApproxEqRelInner(comptime T: type, expected: T, actual: T, tolerance: T) !void {
328325
switch (@typeInfo(T)) {
329-
.float => if (!math.approxEqRel(T, expected, actual, tolerance)) {
326+
.float, .comptime_float => if (!math.approxEqRel(T, expected, actual, tolerance)) {
330327
print("actual {}, not within relative tolerance {} of expected {}\n", .{ actual, tolerance, expected });
331328
return error.TestExpectedApproxEqRel;
332329
},
333-
334-
.comptime_float => @compileError("Cannot approximately compare two comptime_float values"),
335-
336330
else => @compileError("Unable to compare non floating point values"),
337331
}
338332
}
339333

340334
test expectApproxEqRel {
341-
inline for ([_]type{ f16, f32, f64, f128 }) |T| {
335+
inline for ([_]type{ f16, f32, f64, f128, comptime_float }) |T| {
342336
const eps_value = comptime math.floatEps(T);
343337
const sqrt_eps_value = comptime @sqrt(eps_value);
344338

0 commit comments

Comments
 (0)