37.123456789 in, 98.8222222202 out. The celsius-to-fahrenheit crate is supposed to round to two decimal places, so the answer should be 98.82.
The crate’s test suite passes anyway.
Here is the shipped code:
impl Solution {
pub fn celsius_to_fahrenheit(celsius: f64) -> f64 {
(Self::celsius_to_fahrenheit_(celsius) * 100.0) / 100.0
}
fn celsius_to_fahrenheit_(celsius: f64) -> f64 {
(celsius * 9.0 / 5.0) + 32.0
}
fn fahrenheit_to_celsius_(farenheit: f64) -> f64 {
(farenheit - 32.0) * (5.0 / 9.0)
}
}
Scale up, scale back down
Multiplying by 100 and then dividing by 100 is a scale up and a scale back down. It is not rounding, and the fact that it looks like the standard rounding idiom is exactly what makes it dangerous.
I measured it. For 37.123456789, the raw value of the formula is 98.8222222202. The shipped function returns 98.8222222202.
Those are the same number, and they are the same number in the stronger sense: the raw f64 and the scaled f64 have identical bit patterns, 4636654412611351405 for both. Nothing was rounded. The multiply and divide cancelled.
The cancellation is not guaranteed. Across 2,000,000 pseudo-random Celsius values, the round trip x * 100.0 / 100.0 produced a bit-identical result 1,706,272 times, about 85 percent of the time. The other 15 percent landed a tiny distance away from where it started, for example raw = -459189.94180000003 against scaled = -459189.9418, a gap of about 6e-11.
So the operation is a no-op that is usually exact and occasionally lossy, and it is never a rounding no matter which of the two it is.
A real two-decimal round is a different operation:
(raw * 100.0).round() / 100.0
For the same input that gives 98.82.
The test passes on exact values
The test is this:
#[test]
fn test_celsius_to_fahrenheit() {
for (input, output) in [(0.0, 32.00), (100.0, 212.00), (37.0, 98.60)] {
assert_eq!(Solution::celsius_to_fahrenheit(input), output);
}
}
Every one of those three inputs has an answer that is exact at two decimal places: 32, 212, 98.6. The formula computes 98.6 for 37.0, and the literal 98.60 is the nearest f64 to the number 98.6, so the two have the same bits, 4636638775112787558.
assert_eq! compares them and finds them equal.
The assertion passes because the arithmetic was exact, not because anything was rounded. There is no input in that list that could tell a rounding implementation from a non-rounding one. Swap in 37.123456789 and the two implementations separate immediately: the shipped function returns 98.8222222202, and assert_eq!(shipped, 98.82) is false.
That is the failure mode. A test named test_celsius_to_fahrenheit that never exercises the only interesting thing the function claims to do.
f64 cannot carry decimal places
In Rust, assert_eq! on two f64 values is a bit-for-bit comparison. There is no tolerance, no “close enough”, no epsilon. That is a good default and I would not change it.
But it means the test’s expectation 98.60 is not a statement about decimal places at all. It is a statement about one particular f64 bit pattern, and the fact that the source code spells it 98.60 rather than 98.6 is a property of the tokenizer, not of the value.
The deeper problem is that f64 has no decimal precision. Two decimal places is not something the return type can carry, so the requirement lives entirely in the test, and the test is the wrong place for it because the test can only compare two f64 values to each other.
A function that “returns a value rounded to two decimal places” is a promise about a representation the signature does not describe. The honest Rust shape for this problem is a pair: the numeric value and a formatted string, or a fixed-point integer with a documented scale.
The function nobody calls
fahrenheit_to_celsius_ is never called from anywhere in the crate. It is dead code that happens to be correct.
The crate opens with #![allow(unused)]. I removed it and rebuilt to see what it was covering:
warning: struct `Solution` is never constructed
warning: associated functions `celsius_to_fahrenheit`, `celsius_to_fahrenheit_`,
and `fahrenheit_to_celsius_` are never used
The struct Solution warning is a property of how these crates are built: a binary whose entry point is fn main() never constructs the type the grading harness will call. That one is unavoidable and the allow is the right answer for it.
The second warning is a different thing. It names three functions that no code path reaches, and one of them is the Fahrenheit converter, which was written for a conversion the problem never asked for and then never wired up. The parameter is spelled farenheit, which is what happens to a function nobody goes back to.
So the attribute at the top of the file is doing two jobs at once: one legitimate, one covering up a function that should have been deleted or called.
The dead code is not causing the wrong answer. It is costing the file the ability to tell you when something stops being used.
A version that rounds, and says so
If I were rewriting this crate today, the function would round, and it would say that it rounds:
pub fn celsius_to_fahrenheit(celsius: f64) -> f64 {
let fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
(fahrenheit * 100.0).round() / 100.0
}
And the test would have one input in it that has more than two decimals.
I have not made either change. The crate still scales up and scales straight back down, and the test still has three inputs that are all exact. When I get back to it, the first thing I add is the one input with more decimals than the promise allows, because that is the input that makes the test mean something.