https://issues.dlang.org/show_bug.cgi?id=13369
Issue ID: 13369
Summary: std.math.iLog10
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: Phobos
Assignee: [email protected]
Reporter: [email protected]
I suggest to add to Phobos an overloaded function like this:
uint iLog10(in uint x) pure nothrow @safe @nogc
in {
assert(x > 0);
} body {
return (x >= 1_000_000_000) ? 9 :
(x >= 100_000_000) ? 8 :
(x >= 10_000_000) ? 7 :
(x >= 1_000_000) ? 6 :
(x >= 100_000) ? 5 :
(x >= 10_000) ? 4 :
(x >= 1_000) ? 3 :
(x >= 100) ? 2 :
(x >= 10) ? 1 : 0;
}
uint iLog10(in ulong x) pure nothrow @safe @nogc
in {
assert(x > 0);
} body {
return (x >= 10_000_000_000_000_000_000UL) ? 19 :
(x >= 1_000_000_000_000_000_000UL) ? 18 :
(x >= 100_000_000_000_000_000UL) ? 17 :
(x >= 10_000_000_000_000_000UL) ? 16 :
(x >= 1_000_000_000_000_000UL) ? 15 :
(x >= 100_000_000_000_000UL) ? 14 :
(x >= 10_000_000_000_000UL) ? 13 :
(x >= 1_000_000_000_000UL) ? 12 :
(x >= 100_000_000_000UL) ? 11 :
(x >= 10_000_000_000UL) ? 10 :
(x >= 1_000_000_000UL) ? 9 :
(x >= 100_000_000UL) ? 8 :
(x >= 10_000_000UL) ? 7 :
(x >= 1_000_000UL) ? 6 :
(x >= 100_000UL) ? 5 :
(x >= 10_000UL) ? 4 :
(x >= 1_000UL) ? 3 :
(x >= 100UL) ? 2 :
(x >= 10UL) ? 1 : 0;
}
See also Issue 9762 .
--