DEV Community

Michael Lip
Michael Lip

Posted on • Originally published at zovo.one

Unit Conversion Errors Have Crashed Spacecraft (And Probably Your Code)

In 1999, NASA's Mars Climate Orbiter burned up in the Martian atmosphere because one team used pound-force seconds and another team used newton-seconds. A $125 million spacecraft destroyed by a unit conversion error. The software worked perfectly. The units were wrong.

This is the most expensive unit conversion bug in history, but smaller versions happen constantly in software engineering. API returns data in meters, your code assumes feet. A database stores weight in kilograms, the UI displays pounds without conversion. A recipe calls for 350 degrees and you don't check whether it means Fahrenheit or Celsius.

Why unit bugs are so dangerous

Unit conversion errors are uniquely dangerous because:

They compile and run. A number is a number to the computer. It does not know or care whether 350 represents degrees Fahrenheit or degrees Celsius. There is no type error, no runtime exception, no warning.

They produce plausible results. Converting 100 kg to pounds by multiplying by 2 (instead of 2.205) gives 200 lbs instead of 220.5 lbs. Both numbers are plausible weights. The error does not look obviously wrong unless you know the correct answer.

They propagate. A unit error in an intermediate calculation corrupts every downstream result. If your velocity calculation is in m/s but you treat it as km/h, every distance, time, and acceleration calculation derived from it is wrong by a factor of 3.6.

The common conversion categories

Length: 1 inch = 2.54 cm (exact). 1 foot = 0.3048 m (exact). 1 mile = 1.60934 km. 1 nautical mile = 1.852 km.

Weight/Mass: 1 pound = 0.453592 kg. 1 ounce = 28.3495 g. 1 stone = 6.35029 kg. 1 metric ton = 1000 kg.

Volume: 1 US gallon = 3.78541 liters. 1 Imperial gallon = 4.54609 liters. 1 US fluid ounce = 29.5735 mL. The US/Imperial gallon difference is a common source of errors.

Speed: 1 mph = 1.60934 km/h = 0.44704 m/s. 1 knot = 1.852 km/h.

Area: 1 acre = 4,046.86 m^2. 1 hectare = 10,000 m^2 = 2.471 acres. 1 sq mile = 640 acres.

Data: 1 KB = 1,000 bytes (decimal) or 1,024 bytes (binary, properly called KiB). This distinction matters. A "500 GB" hard drive is 500,000,000,000 bytes, which is 465.66 GiB. The "missing" space is not missing. It is a unit discrepancy.

Pressure: 1 atm = 101,325 Pa = 14.696 psi = 760 mmHg. Used in weather, HVAC, and tire inflation.

The engineering approach to units

The safest way to handle units in code is to standardize on SI units internally and convert only at the boundaries (input and output).

// Store internally as meters
function feetToMeters(feet) { return feet * 0.3048; }
function metersToFeet(meters) { return meters / 0.3048; }

// All internal calculations use meters
function calculateArea(lengthMeters, widthMeters) {
  return lengthMeters * widthMeters; // square meters
}
Enter fullscreen mode Exit fullscreen mode

Libraries like js-quantities or Rust's type system can enforce units at the type level, preventing you from adding meters to seconds. The Mars Orbiter crash would have been caught at compile time with a units-aware type system.

Dimensional analysis

The most reliable manual conversion technique is dimensional analysis: multiply by conversion factors expressed as fractions where the numerator and denominator are equal.

Convert 60 mph to m/s:

60 miles/hour * 1609.34 meters/mile * 1 hour/3600 seconds
= 60 * 1609.34 / 3600
= 26.82 m/s
Enter fullscreen mode Exit fullscreen mode

The key: the units cancel algebraically. Miles cancel, hours cancel, leaving meters/seconds. If your result has the wrong units, you set up the fractions incorrectly.

I built a unit converter at zovo.one/free-tools/unit-converter that covers all common categories with precise conversion factors. Select a category, enter a value and source unit, and see conversions to all related units instantly. It is the fastest way to avoid a conversion error that might not crash a spacecraft but will definitely cause a bug.

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

Top comments (0)