diff --git a/lib/std/math/ln.zig b/lib/std/math/ln.zig index e0ce32a7e158191203721ce6345130ee4b605167..ce7cb3d8824a4631da8294622a0f25c068190241 100644 --- a/lib/std/math/ln.zig +++ b/lib/std/math/ln.zig @@ -36,8 +36,9 @@ pub fn ln(x: anytype) @TypeOf(x) { .ComptimeInt => { return @as(comptime_int, math.floor(ln_64(@as(f64, x)))); }, - .Int => { - return @as(T, math.floor(ln_64(@as(f64, x)))); + .Int => |IntType| switch (IntType.signedness) { + .signed => return @compileError("ln not implemented for signed integers"), + .unsigned => return @as(T, math.floor(ln_64(@as(f64, x)))), }, else => @compileError("ln not implemented for " ++ @typeName(T)), } diff --git a/lib/std/math/log.zig b/lib/std/math/log.zig index ef4d4bbb97365e21f7bf55a87280838cedfeb8be..1a8101e67a9cb326c7f09a1d234cb5000774912f 100644 --- a/lib/std/math/log.zig +++ b/lib/std/math/log.zig @@ -31,9 +31,11 @@ pub fn log(comptime T: type, base: T, x: T) T { .ComptimeInt => { return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base))); }, - .Int => { - // TODO implement integer log without using float math - return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base))); + + // TODO implement integer log without using float math + .Int => |IntType| switch (IntType.signedness) { + .signed => return @compileError("log not implemented for signed integers"), + .unsigned => return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base))), }, .Float => { @@ -53,7 +55,7 @@ pub fn log(comptime T: type, base: T, x: T) T { test "math.log integer" { expect(log(u8, 2, 0x1) == 0); expect(log(u8, 2, 0x2) == 1); - expect(log(i16, 2, 0x72) == 6); + expect(log(u16, 2, 0x72) == 6); expect(log(u32, 2, 0xFFFFFF) == 23); expect(log(u64, 2, 0x7FF0123456789ABC) == 62); } diff --git a/lib/std/math/log10.zig b/lib/std/math/log10.zig index 719e0cf51d9a7f48f969bc0d7ee9f0ef0252af1b..a8211a72709d9f5f5b9266505d33ca4a9d1b9f93 100644 --- a/lib/std/math/log10.zig +++ b/lib/std/math/log10.zig @@ -37,8 +37,9 @@ pub fn log10(x: anytype) @TypeOf(x) { .ComptimeInt => { return @as(comptime_int, math.floor(log10_64(@as(f64, x)))); }, - .Int => { - return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x)))); + .Int => |IntType| switch (IntType.signedness) { + .signed => return @compileError("log10 not implemented for signed integers"), + .unsigned => return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x)))), }, else => @compileError("log10 not implemented for " ++ @typeName(T)), } diff --git a/lib/std/math/log2.zig b/lib/std/math/log2.zig index c44672751ec8ee17832f1559138a04471cf3da94..76bc79407c912b98ab427159ad4a850cfc9dcfb4 100644 --- a/lib/std/math/log2.zig +++ b/lib/std/math/log2.zig @@ -43,8 +43,9 @@ pub fn log2(x: anytype) @TypeOf(x) { }) : (result += 1) {} return result; }, - .Int => { - return math.log2_int(T, x); + .Int => |IntType| switch (IntType.signedness) { + .signed => return @compileError("log2 not implemented for signed integers"), + .unsigned => return math.log2_int(T, x), }, else => @compileError("log2 not implemented for " ++ @typeName(T)), } diff --git a/lib/std/math/sqrt.zig b/lib/std/math/sqrt.zig index 38609115d8454ce63f32e7097aa40db8e5608255..394164ff5450dfd37bab8d74474f5a8045c86a46 100644 --- a/lib/std/math/sqrt.zig +++ b/lib/std/math/sqrt.zig @@ -31,7 +31,10 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) { } return @as(T, sqrt_int(u128, x)); }, - .Int => return sqrt_int(T, x), + .Int => |IntType| switch (IntType.signedness) { + .signed => return @compileError("sqrt not implemented for signed integers"), + .unsigned => return sqrt_int(T, x), + }, else => @compileError("sqrt not implemented for " ++ @typeName(T)), } }