Skip to content

Commit 36d5392

Browse files
LewisGaulandrewrk
authored andcommitted
Add tests for log10()
1 parent 650e358 commit 36d5392

File tree

1 file changed

+64
-27
lines changed

1 file changed

+64
-27
lines changed

lib/compiler_rt/log10.zig

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
const std = @import("std");
88
const builtin = @import("builtin");
99
const math = std.math;
10-
const testing = std.testing;
10+
const expect = std.testing.expect;
11+
const expectEqual = std.testing.expectEqual;
1112
const maxInt = std.math.maxInt;
1213
const arch = builtin.cpu.arch;
1314
const common = @import("common.zig");
@@ -187,38 +188,74 @@ pub fn log10l(x: c_longdouble) callconv(.c) c_longdouble {
187188
}
188189
}
189190

190-
test "log10_32" {
191-
const epsilon = 0.000001;
191+
test "log10f() special" {
192+
try expectEqual(log10f(0.0), -math.inf(f32));
193+
try expectEqual(log10f(-0.0), -math.inf(f32));
194+
try expectEqual(log10f(1.0), 0.0);
195+
try expectEqual(log10f(10.0), 1.0);
196+
try expectEqual(log10f(0.1), -1.0);
197+
try expectEqual(log10f(math.inf(f32)), math.inf(f32));
198+
try expect(math.isNan(log10f(-1.0)));
199+
try expect(math.isNan(log10f(-math.inf(f32))));
200+
try expect(math.isNan(log10f(math.nan(f32))));
201+
try expect(math.isNan(log10f(math.snan(f32))));
202+
}
192203

193-
try testing.expect(math.approxEqAbs(f32, log10f(0.2), -0.698970, epsilon));
194-
try testing.expect(math.approxEqAbs(f32, log10f(0.8923), -0.049489, epsilon));
195-
try testing.expect(math.approxEqAbs(f32, log10f(1.5), 0.176091, epsilon));
196-
try testing.expect(math.approxEqAbs(f32, log10f(37.45), 1.573452, epsilon));
197-
try testing.expect(math.approxEqAbs(f32, log10f(89.123), 1.94999, epsilon));
198-
try testing.expect(math.approxEqAbs(f32, log10f(123123.234375), 5.09034, epsilon));
204+
test "log10f() sanity" {
205+
try expect(math.isNan(log10f(-0x1.0223a0p+3)));
206+
try expectEqual(log10f(0x1.161868p+2), 0x1.46a9bcp-1);
207+
try expect(math.isNan(log10f(-0x1.0c34b4p+3)));
208+
try expect(math.isNan(log10f(-0x1.a206f0p+2)));
209+
try expectEqual(log10f(0x1.288bbcp+3), 0x1.ef1300p-1);
210+
try expectEqual(log10f(0x1.52efd0p-1), -0x1.6ee6dcp-3); // Disagrees with GCC in last bit
211+
try expect(math.isNan(log10f(-0x1.a05cc8p-2)));
212+
try expectEqual(log10f(0x1.1f9efap-1), -0x1.0075ccp-2);
213+
try expectEqual(log10f(0x1.8c5db0p-1), -0x1.c75df8p-4);
214+
try expect(math.isNan(log10f(-0x1.5b86eap-1)));
199215
}
200216

201-
test "log10_64" {
202-
const epsilon = 0.000001;
217+
test "log10f() boundary" {
218+
try expectEqual(log10f(0x1.fffffep+127), 0x1.344136p+5); // Max input value
219+
try expectEqual(log10f(0x1p-149), -0x1.66d3e8p+5); // Min positive input value
220+
try expect(math.isNan(log10f(-0x1p-149))); // Min negative input value
221+
try expectEqual(log10f(0x1.000002p+0), 0x1.bcb7b0p-25); // Last value before result reaches +0
222+
try expectEqual(log10f(0x1.fffffep-1), -0x1.bcb7b2p-26); // Last value before result reaches -0
223+
try expectEqual(log10f(0x1p-126), -0x1.2f7030p+5); // First subnormal
224+
try expect(math.isNan(log10f(-0x1p-126))); // First negative subnormal
225+
}
203226

204-
try testing.expect(math.approxEqAbs(f64, log10(0.2), -0.698970, epsilon));
205-
try testing.expect(math.approxEqAbs(f64, log10(0.8923), -0.049489, epsilon));
206-
try testing.expect(math.approxEqAbs(f64, log10(1.5), 0.176091, epsilon));
207-
try testing.expect(math.approxEqAbs(f64, log10(37.45), 1.573452, epsilon));
208-
try testing.expect(math.approxEqAbs(f64, log10(89.123), 1.94999, epsilon));
209-
try testing.expect(math.approxEqAbs(f64, log10(123123.234375), 5.09034, epsilon));
227+
test "log10() special" {
228+
try expectEqual(log10(0.0), -math.inf(f64));
229+
try expectEqual(log10(-0.0), -math.inf(f64));
230+
try expectEqual(log10(1.0), 0.0);
231+
try expectEqual(log10(10.0), 1.0);
232+
try expectEqual(log10(0.1), -1.0);
233+
try expectEqual(log10(math.inf(f64)), math.inf(f64));
234+
try expect(math.isNan(log10(-1.0)));
235+
try expect(math.isNan(log10(-math.inf(f64))));
236+
try expect(math.isNan(log10(math.nan(f64))));
237+
try expect(math.isNan(log10(math.snan(f64))));
210238
}
211239

212-
test "log10_32.special" {
213-
try testing.expect(math.isPositiveInf(log10f(math.inf(f32))));
214-
try testing.expect(math.isNegativeInf(log10f(0.0)));
215-
try testing.expect(math.isNan(log10f(-1.0)));
216-
try testing.expect(math.isNan(log10f(math.nan(f32))));
240+
test "log10() sanity" {
241+
try expect(math.isNan(log10(-0x1.02239f3c6a8f1p+3)));
242+
try expectEqual(log10(0x1.161868e18bc67p+2), 0x1.46a9bd1d2eb87p-1);
243+
try expect(math.isNan(log10(-0x1.0c34b3e01e6e7p+3)));
244+
try expect(math.isNan(log10(-0x1.a206f0a19dcc4p+2)));
245+
try expectEqual(log10(0x1.288bbb0d6a1e6p+3), 0x1.ef12fff994862p-1);
246+
try expectEqual(log10(0x1.52efd0cd80497p-1), -0x1.6ee6db5a155cbp-3);
247+
try expect(math.isNan(log10(-0x1.a05cc754481d1p-2)));
248+
try expectEqual(log10(0x1.1f9ef934745cbp-1), -0x1.0075cda79d321p-2);
249+
try expectEqual(log10(0x1.8c5db097f7442p-1), -0x1.c75df6442465ap-4);
250+
try expect(math.isNan(log10(-0x1.5b86ea8118a0ep-1)));
217251
}
218252

219-
test "log10_64.special" {
220-
try testing.expect(math.isPositiveInf(log10(math.inf(f64))));
221-
try testing.expect(math.isNegativeInf(log10(0.0)));
222-
try testing.expect(math.isNan(log10(-1.0)));
223-
try testing.expect(math.isNan(log10(math.nan(f64))));
253+
test "log10() boundary" {
254+
try expectEqual(log10(0x1.fffffffffffffp+1023), 0x1.34413509f79ffp+8); // Max input value
255+
try expectEqual(log10(0x1p-1074), -0x1.434e6420f4374p+8); // Min positive input value
256+
try expect(math.isNan(log10(-0x1p-1074))); // Min negative input value
257+
try expectEqual(log10(0x1.0000000000001p+0), 0x1.bcb7b1526e50dp-54); // Last value before result reaches +0
258+
try expectEqual(log10(0x1.fffffffffffffp-1), -0x1.bcb7b1526e50fp-55); // Last value before result reaches -0
259+
try expectEqual(log10(0x1p-1022), -0x1.33a7146f72a42p+8); // First subnormal
260+
try expect(math.isNan(log10(-0x1p-1022))); // First negative subnormal
224261
}

0 commit comments

Comments
 (0)