DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Why Speed Conversion Bugs Cause Real Engineering Failures

In 1999, NASA lost the Mars Climate Orbiter because one team used imperial units and another used metric. The spacecraft entered the Martian atmosphere at the wrong angle and disintegrated. A $327 million mission, destroyed by a unit conversion error. Most of us are not sending probes to Mars, but unit conversion mistakes still cause real problems in software.

The conversion factors

Kilometers per hour to miles per hour:

mph = kmh * 0.621371
Enter fullscreen mode Exit fullscreen mode

Miles per hour to kilometers per hour:

kmh = mph * 1.60934
Enter fullscreen mode Exit fullscreen mode

Those two numbers, 0.621371 and 1.60934, are the only constants you need. They are exact reciprocals of each other (within rounding). Memorize either one and you can derive the other.

The mental math shortcut

If you need a rough conversion and don't have a calculator handy, here is the trick I use: multiply by 0.6 for km/h to mph, or multiply by 1.6 for mph to km/h.

100 km/h is roughly 60 mph. 60 mph is roughly 96 km/h. Close enough for everyday use.

For even faster estimation, divide by 8 and multiply by 5 for km/h to mph. Or divide by 5 and multiply by 8 for the reverse. This works because 5/8 = 0.625, which is close to the actual factor of 0.621371.

Where this matters in code

If you are building any application that deals with speed, distance, or navigation, unit conversion is a constant concern. Here are the scenarios I see most often.

GPS and mapping applications. GPS devices report speed in meters per second. To display km/h, multiply by 3.6. To display mph, multiply by 2.23694. Getting this wrong means your speedometer shows the wrong number, which in a driving application is a liability issue.

function metersPerSecondTo(mps, unit) {
  if (unit === 'kmh') return mps * 3.6;
  if (unit === 'mph') return mps * 2.23694;
  return mps;
}
Enter fullscreen mode Exit fullscreen mode

Fitness applications. Runners in the US think in miles. Runners in Europe think in kilometers. A pace calculator that doesn't convert correctly between min/km and min/mile will frustrate half its users. The conversion is:

function minPerKmToMinPerMile(minPerKm) {
  return minPerKm * 1.60934;
}
Enter fullscreen mode Exit fullscreen mode

A 5:00 min/km pace is an 8:03 min/mile pace. Not 8:00 even. That three-second difference matters to competitive runners.

Weather APIs. Wind speed in weather data can arrive in m/s, km/h, mph, or knots depending on the data source. The National Weather Service API uses mph. Open-Meteo defaults to km/h. If you are aggregating from multiple sources, you need to normalize to a single unit before comparing or averaging.

The floating-point trap

Here is a subtle bug I have seen in production code:

const speedKmh = 100;
const speedMph = speedKmh * 0.621371;
const backToKmh = speedMph * 1.60934;
console.log(backToKmh); // 99.99999...
console.log(speedKmh === backToKmh); // false
Enter fullscreen mode Exit fullscreen mode

Round-trip conversion with floating-point arithmetic rarely returns the exact original value. If your code compares speeds for equality after conversion, it will fail. Always use a tolerance:

function speedEquals(a, b, tolerance = 0.01) {
  return Math.abs(a - b) < tolerance;
}
Enter fullscreen mode Exit fullscreen mode

Storing speed values

In databases, I always store speed in a single canonical unit (usually m/s, the SI standard) and convert on display. This avoids the nightmare of having some rows in km/h, others in mph, and no column indicating which is which. I have inherited databases with exactly this problem. It took weeks to untangle.

-- Store in m/s, convert on read
SELECT speed_ms * 3.6 AS speed_kmh,
       speed_ms * 2.23694 AS speed_mph
FROM vehicle_telemetry;
Enter fullscreen mode Exit fullscreen mode

Common reference points

These are worth memorizing for sanity-checking conversions:

  • 100 km/h = 62.14 mph (highway speed in most countries)
  • 60 mph = 96.56 km/h (US highway speed)
  • 120 km/h = 74.56 mph (EU motorway speed)
  • 30 mph = 48.28 km/h (UK urban speed limit)
  • 200 km/h = 124.27 mph (high-speed rail)

If a conversion result does not roughly match these reference points, something is wrong.

The tool

When I need quick, accurate speed conversions during development or documentation, I use zovo.one/free-tools/kmh-mph-converter. It handles the precision correctly and shows both directions instantly. Faster than typing the formula into a REPL, and the result is always right.

Unit conversion seems trivial until it isn't. Build the habit of storing in a canonical unit, converting at the boundary, and never comparing converted floating-point values for equality. Your future self will thank you.


I'm Michael Lip. I build free developer tools at zovo.one. 500+ tools, all private, all free.

Top comments (0)