aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/floatop.zig
diff options
context:
space:
mode:
authorJohn Schmidt <john.schmidt.h@gmail.com>2022-02-04 20:21:15 +0100
committerAndrew Kelley <andrew@ziglang.org>2022-02-07 16:52:19 -0700
commit722d4a11bbba4052558f6f69b7e710d1206f3355 (patch)
tree5c98c05bb228555a7b7e9bf3c71d48538c3b84e4 /test/behavior/floatop.zig
parentdd49ed1c642d917af40bf4a0e03d013f40b3903b (diff)
downloadzig-722d4a11bbba4052558f6f69b7e710d1206f3355.tar.gz
zig-722d4a11bbba4052558f6f69b7e710d1206f3355.zip
stage2: implement @sqrt for f{16,32,64}
Support for f128, comptime_float, and c_longdouble require improvements to compiler_rt and will implemented in a later PR. Some of the code in this commit could be made more generic, for instance `llvm.airSqrt` could probably be `llvm.airUnaryMath`, but let's cross that bridge when we get to it.
Diffstat (limited to 'test/behavior/floatop.zig')
-rw-r--r--test/behavior/floatop.zig42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/behavior/floatop.zig b/test/behavior/floatop.zig
index 20ef4ce68d..7807c690f6 100644
--- a/test/behavior/floatop.zig
+++ b/test/behavior/floatop.zig
@@ -72,3 +72,45 @@ test "negative f128 floatToInt at compile-time" {
var b = @floatToInt(i64, a);
try expect(@as(i64, -2) == b);
}
+
+test "@sqrt" {
+ comptime try testSqrt();
+ try testSqrt();
+}
+
+fn testSqrt() !void {
+ {
+ var a: f16 = 4;
+ try expect(@sqrt(a) == 2);
+ }
+ {
+ var a: f32 = 9;
+ try expect(@sqrt(a) == 3);
+ var b: f32 = 1.1;
+ try expect(math.approxEqAbs(f32, @sqrt(b), 1.0488088481701516, epsilon));
+ }
+ {
+ var a: f64 = 25;
+ try expect(@sqrt(a) == 5);
+ }
+}
+
+test "more @sqrt f16 tests" {
+ // TODO these are not all passing at comptime
+ try expect(@sqrt(@as(f16, 0.0)) == 0.0);
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 2.0)), 1.414214, epsilon));
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 3.6)), 1.897367, epsilon));
+ try expect(@sqrt(@as(f16, 4.0)) == 2.0);
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 7.539840)), 2.745877, epsilon));
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 19.230934)), 4.385309, epsilon));
+ try expect(@sqrt(@as(f16, 64.0)) == 8.0);
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 64.1)), 8.006248, epsilon));
+ try expect(math.approxEqAbs(f16, @sqrt(@as(f16, 8942.230469)), 94.563370, epsilon));
+
+ // special cases
+ try expect(math.isPositiveInf(@sqrt(@as(f16, math.inf(f16)))));
+ try expect(@sqrt(@as(f16, 0.0)) == 0.0);
+ try expect(@sqrt(@as(f16, -0.0)) == -0.0);
+ try expect(math.isNan(@sqrt(@as(f16, -1.0))));
+ try expect(math.isNan(@sqrt(@as(f16, math.nan(f16)))));
+}