authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2021-04-03 19:27:37+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-03 19:27:37+02:00
log6fc822a9481c0d2c8b37f26f41be603dd77ab5a4
tree0616fe6a4ecc6e35b2c7390363909a8d1d635f2a
parent2f07d76eee37442f53c294f53b38b11dfb1cd4da
parentd4dc2eb8076a5aa5cf2eb94c7b8407c94337bff8
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8410 from antlilja/remove-undefined-math

Make sqrt and log functions undefined for signed integer types

5 files changed, 19 insertions(+), 11 deletions(-)

lib/std/math/ln.zig+3-2
......@@ -36,8 +36,9 @@ pub fn ln(x: anytype) @TypeOf(x) {
3636 .ComptimeInt => {
3737 return @as(comptime_int, math.floor(ln_64(@as(f64, x))));
3838 },
39 .Int => {
40 return @as(T, math.floor(ln_64(@as(f64, x))));
39 .Int => |IntType| switch (IntType.signedness) {
40 .signed => return @compileError("ln not implemented for signed integers"),
41 .unsigned => return @as(T, math.floor(ln_64(@as(f64, x)))),
4142 },
4243 else => @compileError("ln not implemented for " ++ @typeName(T)),
4344 }
lib/std/math/log.zig+6-4
......@@ -31,9 +31,11 @@ pub fn log(comptime T: type, base: T, x: T) T {
3131 .ComptimeInt => {
3232 return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base)));
3333 },
34 .Int => {
35 // TODO implement integer log without using float math
36 return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));
34
35 // TODO implement integer log without using float math
36 .Int => |IntType| switch (IntType.signedness) {
37 .signed => return @compileError("log not implemented for signed integers"),
38 .unsigned => return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base))),
3739 },
3840
3941 .Float => {
......@@ -53,7 +55,7 @@ pub fn log(comptime T: type, base: T, x: T) T {
5355test "math.log integer" {
5456 expect(log(u8, 2, 0x1) == 0);
5557 expect(log(u8, 2, 0x2) == 1);
56 expect(log(i16, 2, 0x72) == 6);
58 expect(log(u16, 2, 0x72) == 6);
5759 expect(log(u32, 2, 0xFFFFFF) == 23);
5860 expect(log(u64, 2, 0x7FF0123456789ABC) == 62);
5961}
lib/std/math/log10.zig+3-2
......@@ -37,8 +37,9 @@ pub fn log10(x: anytype) @TypeOf(x) {
3737 .ComptimeInt => {
3838 return @as(comptime_int, math.floor(log10_64(@as(f64, x))));
3939 },
40 .Int => {
41 return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));
40 .Int => |IntType| switch (IntType.signedness) {
41 .signed => return @compileError("log10 not implemented for signed integers"),
42 .unsigned => return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x)))),
4243 },
4344 else => @compileError("log10 not implemented for " ++ @typeName(T)),
4445 }
lib/std/math/log2.zig+3-2
......@@ -43,8 +43,9 @@ pub fn log2(x: anytype) @TypeOf(x) {
4343 }) : (result += 1) {}
4444 return result;
4545 },
46 .Int => {
47 return math.log2_int(T, x);
46 .Int => |IntType| switch (IntType.signedness) {
47 .signed => return @compileError("log2 not implemented for signed integers"),
48 .unsigned => return math.log2_int(T, x),
4849 },
4950 else => @compileError("log2 not implemented for " ++ @typeName(T)),
5051 }
lib/std/math/sqrt.zig+4-1
......@@ -31,7 +31,10 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {
3131 }
3232 return @as(T, sqrt_int(u128, x));
3333 },
34 .Int => return sqrt_int(T, x),
34 .Int => |IntType| switch (IntType.signedness) {
35 .signed => return @compileError("sqrt not implemented for signed integers"),
36 .unsigned => return sqrt_int(T, x),
37 },
3538 else => @compileError("sqrt not implemented for " ++ @typeName(T)),
3639 }
3740}