authorgravatar for hemisputnik@proton.mehemisputnik <hemisputnik@proton.me> 2026-02-25 10:48:24+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-25 18:57:58+01:00
log2f8e66080580c1ef3814cc4e2f27ae418d8d6e0c
tree65c921de0d7f903e12be55148a5bfd3be4cce24a
parent608b07a3d79554dc825292878dd4167dec143f23

std.math.log10: handle comptime_int inputs correctly

also add a few tests for comptime types fixes #31333

1 files changed, 13 insertions(+), 1 deletions(-)

lib/std/math/log10.zig+13-1
......@@ -17,7 +17,12 @@ pub fn log10(x: anytype) @TypeOf(x) {
1717 },
1818 .float => return @log10(x),
1919 .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)));
2126 },
2227 .int => |IntType| switch (IntType.signedness) {
2328 .signed => @compileError("log10 not implemented for signed integers"),
......@@ -156,3 +161,10 @@ test log10_int {
156161 try testing.expectEqual(max_exponent, log10_int(@as(T, std.math.maxInt(T))));
157162 }
158163}
164
165test "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}