authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2021-04-02 14:52:47+02:00
committergravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2021-04-03 13:09:20+02:00
logd4dc2eb8076a5aa5cf2eb94c7b8407c94337bff8
tree1bc54a14575cfa9c5274d9cdb6e40c3055ba312d
parentcc435dab2fbf66f2db339310131608472838c67d
signature Commit is signed but in an unrecognized format.

Compile error for signed integer math

Output compile errors when signed integer types are used on functions where the answer might've been a complex number but that functionality hasn't been implemented. This applies to sqrt, log, log2, log10 and ln. A test which used a signed integer was also changed to use an unsigned integer instead.

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) {...@@ -36,8 +36,9 @@ pub fn ln(x: anytype) @TypeOf(x) {
36 .ComptimeInt => {36 .ComptimeInt => {
37 return @as(comptime_int, math.floor(ln_64(@as(f64, x))));37 return @as(comptime_int, math.floor(ln_64(@as(f64, x))));
38 },38 },
39 .Int => {39 .Int => |IntType| switch (IntType.signedness) {
40 return @as(T, math.floor(ln_64(@as(f64, x))));40 .signed => return @compileError("ln not implemented for signed integers"),
41 .unsigned => return @as(T, math.floor(ln_64(@as(f64, x)))),
41 },42 },
42 else => @compileError("ln not implemented for " ++ @typeName(T)),43 else => @compileError("ln not implemented for " ++ @typeName(T)),
43 }44 }
lib/std/math/log.zig+6-4
...@@ -31,9 +31,11 @@ pub fn log(comptime T: type, base: T, x: T) T {...@@ -31,9 +31,11 @@ pub fn log(comptime T: type, base: T, x: T) T {
31 .ComptimeInt => {31 .ComptimeInt => {
32 return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base)));32 return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base)));
33 },33 },
34 .Int => {34
35 // TODO implement integer log without using float math35 // TODO implement integer log without using float math
36 return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));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))),
37 },39 },
3840
39 .Float => {41 .Float => {
...@@ -53,7 +55,7 @@ pub fn log(comptime T: type, base: T, x: T) T {...@@ -53,7 +55,7 @@ pub fn log(comptime T: type, base: T, x: T) T {
53test "math.log integer" {55test "math.log integer" {
54 expect(log(u8, 2, 0x1) == 0);56 expect(log(u8, 2, 0x1) == 0);
55 expect(log(u8, 2, 0x2) == 1);57 expect(log(u8, 2, 0x2) == 1);
56 expect(log(i16, 2, 0x72) == 6);58 expect(log(u16, 2, 0x72) == 6);
57 expect(log(u32, 2, 0xFFFFFF) == 23);59 expect(log(u32, 2, 0xFFFFFF) == 23);
58 expect(log(u64, 2, 0x7FF0123456789ABC) == 62);60 expect(log(u64, 2, 0x7FF0123456789ABC) == 62);
59}61}
lib/std/math/log10.zig+3-2
...@@ -37,8 +37,9 @@ pub fn log10(x: anytype) @TypeOf(x) {...@@ -37,8 +37,9 @@ pub fn log10(x: anytype) @TypeOf(x) {
37 .ComptimeInt => {37 .ComptimeInt => {
38 return @as(comptime_int, math.floor(log10_64(@as(f64, x))));38 return @as(comptime_int, math.floor(log10_64(@as(f64, x))));
39 },39 },
40 .Int => {40 .Int => |IntType| switch (IntType.signedness) {
41 return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));41 .signed => return @compileError("log10 not implemented for signed integers"),
42 .unsigned => return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x)))),
42 },43 },
43 else => @compileError("log10 not implemented for " ++ @typeName(T)),44 else => @compileError("log10 not implemented for " ++ @typeName(T)),
44 }45 }
lib/std/math/log2.zig+3-2
...@@ -43,8 +43,9 @@ pub fn log2(x: anytype) @TypeOf(x) {...@@ -43,8 +43,9 @@ pub fn log2(x: anytype) @TypeOf(x) {
43 }) : (result += 1) {}43 }) : (result += 1) {}
44 return result;44 return result;
45 },45 },
46 .Int => {46 .Int => |IntType| switch (IntType.signedness) {
47 return math.log2_int(T, x);47 .signed => return @compileError("log2 not implemented for signed integers"),
48 .unsigned => return math.log2_int(T, x),
48 },49 },
49 else => @compileError("log2 not implemented for " ++ @typeName(T)),50 else => @compileError("log2 not implemented for " ++ @typeName(T)),
50 }51 }
lib/std/math/sqrt.zig+4-1
...@@ -31,7 +31,10 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {...@@ -31,7 +31,10 @@ pub fn sqrt(x: anytype) Sqrt(@TypeOf(x)) {
31 }31 }
32 return @as(T, sqrt_int(u128, x));32 return @as(T, sqrt_int(u128, x));
33 },33 },
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 },
35 else => @compileError("sqrt not implemented for " ++ @typeName(T)),38 else => @compileError("sqrt not implemented for " ++ @typeName(T)),
36 }39 }
37}40}