| author | |
| committer | |
| log | d4dc2eb8076a5aa5cf2eb94c7b8407c94337bff8 |
| tree | 1bc54a14575cfa9c5274d9cdb6e40c3055ba312d |
| parent | cc435dab2fbf66f2db339310131608472838c67d |
| signature | Commit is signed but in an unrecognized format. |
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 | 36 | .ComptimeInt => { |
| 37 | 37 | return @as(comptime_int, math.floor(ln_64(@as(f64, x)))); |
| 38 | 38 | }, |
| 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)))), | |
| 41 | 42 | }, |
| 42 | 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 | 31 | .ComptimeInt => { |
| 32 | 32 | return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base))); |
| 33 | 33 | }, |
| 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))), | |
| 37 | 39 | }, |
| 38 | 40 | |
| 39 | 41 | .Float => { |
| ... | ... | @@ -53,7 +55,7 @@ pub fn log(comptime T: type, base: T, x: T) T { |
| 53 | 55 | test "math.log integer" { |
| 54 | 56 | expect(log(u8, 2, 0x1) == 0); |
| 55 | 57 | expect(log(u8, 2, 0x2) == 1); |
| 56 | expect(log(i16, 2, 0x72) == 6); | |
| 58 | expect(log(u16, 2, 0x72) == 6); | |
| 57 | 59 | expect(log(u32, 2, 0xFFFFFF) == 23); |
| 58 | 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 | 37 | .ComptimeInt => { |
| 38 | 38 | return @as(comptime_int, math.floor(log10_64(@as(f64, x)))); |
| 39 | 39 | }, |
| 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)))), | |
| 42 | 43 | }, |
| 43 | 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 | 43 | }) : (result += 1) {} |
| 44 | 44 | return result; |
| 45 | 45 | }, |
| 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), | |
| 48 | 49 | }, |
| 49 | 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 | 31 | } |
| 32 | 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 | 38 | else => @compileError("sqrt not implemented for " ++ @typeName(T)), |
| 36 | 39 | } |
| 37 | 40 | } |