aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/math.zig
diff options
context:
space:
mode:
authorMateusz Radomski <33978857+m-radomski@users.noreply.github.com>2022-02-13 14:37:38 +0100
committerGitHub <noreply@github.com>2022-02-13 15:37:38 +0200
commitb5f8fb85e64022ed1ee59ff70753577839ad41b6 (patch)
tree79972cc2e52fe9c9b54a265434148110debe6d04 /test/behavior/math.zig
parentf22443bb05a6be6c3ade08254f52fdd05eeb2910 (diff)
downloadzig-b5f8fb85e64022ed1ee59ff70753577839ad41b6.tar.gz
zig-b5f8fb85e64022ed1ee59ff70753577839ad41b6.zip
Implement f128 `@rem`
Diffstat (limited to 'test/behavior/math.zig')
-rw-r--r--test/behavior/math.zig60
1 files changed, 58 insertions, 2 deletions
diff --git a/test/behavior/math.zig b/test/behavior/math.zig
index a9000353b8..fe5329ec81 100644
--- a/test/behavior/math.zig
+++ b/test/behavior/math.zig
@@ -782,8 +782,6 @@ test "comptime float rem int" {
}
test "remainder division" {
- if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
-
comptime try remdiv(f16);
comptime try remdiv(f32);
comptime try remdiv(f64);
@@ -798,6 +796,64 @@ fn remdiv(comptime T: type) !void {
try expect(@as(T, 1) == @as(T, 7) % @as(T, 3));
}
+test "float remainder division using @rem" {
+ comptime try frem(f16);
+ comptime try frem(f32);
+ comptime try frem(f64);
+ comptime try frem(f128);
+ try frem(f16);
+ try frem(f32);
+ try frem(f64);
+ try frem(f128);
+}
+
+fn frem(comptime T: type) !void {
+ const epsilon = switch (T) {
+ f16 => 1.0,
+ f32 => 0.001,
+ f64 => 0.00001,
+ f128 => 0.0000001,
+ else => unreachable,
+ };
+
+ try expect(std.math.fabs(@rem(@as(T, 6.9), @as(T, 4.0)) - @as(T, 2.9)) < epsilon);
+ try expect(std.math.fabs(@rem(@as(T, -6.9), @as(T, 4.0)) - @as(T, -2.9)) < epsilon);
+ try expect(std.math.fabs(@rem(@as(T, -5.0), @as(T, 3.0)) - @as(T, -2.0)) < epsilon);
+ try expect(std.math.fabs(@rem(@as(T, 3.0), @as(T, 2.0)) - @as(T, 1.0)) < epsilon);
+ try expect(std.math.fabs(@rem(@as(T, 1.0), @as(T, 2.0)) - @as(T, 1.0)) < epsilon);
+ try expect(std.math.fabs(@rem(@as(T, 0.0), @as(T, 1.0)) - @as(T, 0.0)) < epsilon);
+ try expect(std.math.fabs(@rem(@as(T, -0.0), @as(T, 1.0)) - @as(T, -0.0)) < epsilon);
+}
+
+test "float modulo division using @mod" {
+ comptime try fmod(f16);
+ comptime try fmod(f32);
+ comptime try fmod(f64);
+ comptime try fmod(f128);
+ try fmod(f16);
+ try fmod(f32);
+ try fmod(f64);
+ try fmod(f128);
+}
+
+fn fmod(comptime T: type) !void {
+ const epsilon = switch (T) {
+ f16 => 1.0,
+ f32 => 0.001,
+ f64 => 0.00001,
+ f128 => 0.0000001,
+ else => unreachable,
+ };
+
+ try expect(std.math.fabs(@mod(@as(T, 6.9), @as(T, 4.0)) - @as(T, 2.9)) < epsilon);
+ try expect(std.math.fabs(@mod(@as(T, -6.9), @as(T, 4.0)) - @as(T, 1.1)) < epsilon);
+ try expect(std.math.fabs(@mod(@as(T, -5.0), @as(T, 3.0)) - @as(T, 1.0)) < epsilon);
+ try expect(std.math.fabs(@mod(@as(T, 3.0), @as(T, 2.0)) - @as(T, 1.0)) < epsilon);
+ try expect(std.math.fabs(@mod(@as(T, 1.0), @as(T, 2.0)) - @as(T, 1.0)) < epsilon);
+ try expect(std.math.fabs(@mod(@as(T, 0.0), @as(T, 1.0)) - @as(T, 0.0)) < epsilon);
+ try expect(std.math.fabs(@mod(@as(T, -0.0), @as(T, 1.0)) - @as(T, -0.0)) < epsilon);
+}
+
test "@sqrt" {
try testSqrt(f64, 12.0);
comptime try testSqrt(f64, 12.0);