[Issue 13369] New: std.math.iLog10
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Sun Aug 24 09:09:24 PDT 2014
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: nobody at puremagic.com
Reporter: bearophile_hugs at eml.cc
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 .
--
More information about the Digitalmars-d-bugs
mailing list