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
signaturelock-open 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) {
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}