mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 03:44:46 +01:00
std.math.log10: handle comptime_int inputs correctly
also add a few tests for comptime types fixes #31333
This commit is contained in:
parent
608b07a3d7
commit
2f8e660805
1 changed files with 13 additions and 1 deletions
|
|
@ -17,7 +17,12 @@ pub fn log10(x: anytype) @TypeOf(x) {
|
|||
},
|
||||
.float => return @log10(x),
|
||||
.comptime_int => {
|
||||
return @as(comptime_int, @floor(@log10(@as(f64, x))));
|
||||
const Int = @Int(
|
||||
if (x < 0) .signed else .unsigned,
|
||||
// We let log10_int check if x is 0, for a nicer error message.
|
||||
@max(16, if (x == 0) 0 else 1 + std.math.log2(x)),
|
||||
);
|
||||
return @as(comptime_int, log10_int(@as(Int, x)));
|
||||
},
|
||||
.int => |IntType| switch (IntType.signedness) {
|
||||
.signed => @compileError("log10 not implemented for signed integers"),
|
||||
|
|
@ -156,3 +161,10 @@ test log10_int {
|
|||
try testing.expectEqual(max_exponent, log10_int(@as(T, std.math.maxInt(T))));
|
||||
}
|
||||
}
|
||||
|
||||
test "log10 with comptime types" {
|
||||
try testing.expectEqual(0, log10(2));
|
||||
try testing.expectEqual(2.0, log10(100.0));
|
||||
try testing.expectEqual(2, log10(123));
|
||||
try testing.expectEqual(3.0, log10(1000.0));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue