| ... | @@ -17,7 +17,12 @@ pub fn log10(x: anytype) @TypeOf(x) { | ... | @@ -17,7 +17,12 @@ pub fn log10(x: anytype) @TypeOf(x) { |
| 17 | }, | 17 | }, |
| 18 | .float => return @log10(x), | 18 | .float => return @log10(x), |
| 19 | .comptime_int => { | 19 | .comptime_int => { |
| 20 | return @as(comptime_int, @floor(@log10(@as(f64, x)))); | 20 | const Int = @Int( |
| | 21 | if (x < 0) .signed else .unsigned, |
| | 22 | // We let log10_int check if x is 0, for a nicer error message. |
| | 23 | @max(16, if (x == 0) 0 else 1 + std.math.log2(x)), |
| | 24 | ); |
| | 25 | return @as(comptime_int, log10_int(@as(Int, x))); |
| 21 | }, | 26 | }, |
| 22 | .int => |IntType| switch (IntType.signedness) { | 27 | .int => |IntType| switch (IntType.signedness) { |
| 23 | .signed => @compileError("log10 not implemented for signed integers"), | 28 | .signed => @compileError("log10 not implemented for signed integers"), |
| ... | @@ -156,3 +161,10 @@ test log10_int { | ... | @@ -156,3 +161,10 @@ test log10_int { |
| 156 | try testing.expectEqual(max_exponent, log10_int(@as(T, std.math.maxInt(T)))); | 161 | try testing.expectEqual(max_exponent, log10_int(@as(T, std.math.maxInt(T)))); |
| 157 | } | 162 | } |
| 158 | } | 163 | } |
| | 164 | |
| | 165 | test "log10 with comptime types" { |
| | 166 | try testing.expectEqual(0, log10(2)); |
| | 167 | try testing.expectEqual(2.0, log10(100.0)); |
| | 168 | try testing.expectEqual(2, log10(123)); |
| | 169 | try testing.expectEqual(3.0, log10(1000.0)); |
| | 170 | } |