I ran the shipped digit counter on zero and got -2147483647.
I wrote that counter, commented out the first version, and shipped a second one. The two versions agree on every input the problem allows. They disagree on everything else, and there the second one answers with a number that looks like an answer. The shipped version is the one in this file:
pub fn count_digits(num: i32) -> i32 {
// Iterative
// let mut count = 0;
// let mut n = num;
// while n > 0 {
// count += 1;
// n /= 10;
// }
// count
// Logarithmic
let count = (num as f64).log10().floor() as i32 + 1;
count
}
The shipped version passes every test in the file. All three of them:
assert_eq!(Solution::count_digits(123), 3);
assert_eq!(Solution::count_digits(1234), 4);
assert_eq!(Solution::count_digits(1000000), 7);
For each of those three inputs, the shipped version and the commented-out version give the same answer. That is not an accident of the test values being unremarkable. I swept both implementations across every value the constraint allows and they agree on all of them. The two versions differ only outside the constraint, which is exactly where no test looked.
That agreement is why the wrong answer was easy to miss. I only found it by running both versions on inputs outside the constraint.
Zero and a negative number
The shipped version, run against inputs the problem does not guarantee:
count_digits(0) = -2147483647
count_digits(-123) = 1
Neither of those is a panic. Neither is an error. Both are ordinary i32 values that a caller has no reason to distrust. The one count_digits(0) returns is a number so far from a digit count that it looks like a sentinel, but nothing in the type says it is one.
The commented-out version, restored and run against the same inputs:
iterative(0) = 0
iterative(-123) = 0
Zero is not right for -123 either. The sign is not a digit. But zero is a number a human reads as “nothing to count” rather than as a corrupted value. The iterative version is wrong in the way a guard clause is wrong: it declines the input instead of inventing an answer for it.
Where the float version breaks
The shipped version converts to f64, takes a logarithm, floors it, and casts back. The interesting failure is in that cast, and Rust’s rules for it are specific.
f64::NEG_INFINITY as i32 is i32::MIN, and f64::NAN as i32 is 0. These are not undefined behavior and not a panic. Since Rust 1.45, float-to-integer as casts saturate: values below the range clamp to the minimum, values above clamp to the maximum, and NaN becomes zero.
So for num = 0:
(num as f64) = 0.0
0.0.log10() = -inf
(-inf).floor() = -inf
(-inf) as i32 = -2147483648 (i32::MIN, saturated)
i32::MIN + 1 = -2147483647
For num = -123:
(num as f64) = -123.0
(-123.0).log10() = NaN
NaN.floor() = NaN
NaN as i32 = 0
0 + 1 = 1
Both paths end in a plausible-looking integer. The saturation is what makes this quiet. An unsaturating cast would have been a trap or a wrap, and either would have surfaced the bug at the first call. Saturation turns an invalid computation into a valid-looking one, and the invalidity is gone.
The float version is fine for positive inputs
I swept the shipped implementation against the commented-out one across the entire stated constraint range, 1 <= n <= 10^9, all one billion values:
swept 1..=1_000_000_000
mismatches = 0
Zero mismatches. On this platform, for every input the problem actually allows, the float version gives the right answer.
I checked the powers of ten specifically, because those are where a log10 implementation is most likely to be off by one:
n= 1 log10=0 shipped=1 iterative=1
n= 10 log10=1 shipped=2 iterative=2
n= 100 log10=2 shipped=3 iterative=3
n= 1000 log10=3 shipped=4 iterative=4
n= 10000 log10=4 shipped=5 iterative=5
n= 100000 log10=5 shipped=6 iterative=6
n= 1000000 log10=6 shipped=7 iterative=7
n= 10000000 log10=7 shipped=8 iterative=8
n= 100000000 log10=8 shipped=9 iterative=9
n=1000000000 log10=9 shipped=10 iterative=10
Every one of those is exact. log10(1000.0) is 3.0, not 2.9999999999999996. The floor() is doing its job because the logarithm of an exact power of ten lands on an exact integer here.
This matters because the natural reading of the failure above is “the float approach is broken for powers of ten.” It is not, on this machine. The failure mode is degenerate inputs, and degenerate inputs are outside the constraint. The shipped code does not violate its own specification.
And that is exactly what makes it worse. A spec violation that the compiler catches is cheap. A spec violation that returns a wrong number with no panic is the kind that reaches production and stays there, because every test in the file passes and every caller that respects the constraint gets the right answer.
The version I would ship
The commented-out code is not quite what I would ship either, because it silently returns 0 for a negative input instead of saying something. This is what I would write:
fn count_digits(num: i32) -> i32 {
let mut count = 0;
let mut n = num.unsigned_abs();
if n == 0 {
return 1;
}
while n > 0 {
count += 1;
n /= 10;
}
count
}
unsigned_abs() turns -123 into 123 without the overflow that abs() has on i32::MIN. The n == 0 early return handles the zero case, where the loop as written would count zero digits.
count_digits_fixed(0) = 1
count_digits_fixed(123) = 3
count_digits_fixed(-123) = 3
count_digits_fixed(i32::MIN) = 10
The repository still ships the float version, and the float version is what has been passing its tests this whole time.
The habit I did not have
I wrote a working solution, commented it out, wrote a second one, and shipped the second one because it looked more sophisticated. log10 and floor are more impressive than a division loop. The iterative version was correct for every input that mattered and had no saturation semantics to reason about.
The part I keep coming back to is not that I shipped the worse version. It is that the worse version passed every test in the file, in both debug and release, and gave the wrong answer quietly for the inputs nobody checked. A test suite is a statement about the cases you thought of. 123, 1234, and 1000000 are the cases I thought of, and they are all fine.
The gap between what the code does on the inputs I imagined and what it does on the inputs I did not is not something Rust can close for me. What closed it was writing down what the code actually does and running it, on an input the constraint never mentions.
What I take from it is narrower than a rule: when I comment out a working implementation, the replacement has to earn the comment. The cheapest way I know to check is to run both versions on one input the spec does not mention and write down both answers, before I delete anything.