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
signaturebadge-question-mark 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) {...@@ -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}