DEV Community

Rasika Dangamuwa
Rasika Dangamuwa

Posted on

The Unit Conversion Bugs That Cause Production Incidents (and How to Avoid Them)

In 1999, NASA lost the 25 million Mars Climate Orbiter because ground system software sent thruster performance data in pound-force seconds, while the spacecraft’s navigation software expected Newton-seconds. One system calculated thrust in imperial units; the other interpreted it in metric. The result was a trajectory error that sent the spacecraft into Mars' upper atmosphere, destroying it.

While most of us aren't landing spacecraft on Mars, unit conversion bugs remain one of the most deceptive categories of software failures in modern web applications, microservices, and frontend interfaces. They rarely crash a system with a clean exception; instead, they fail quietly, corrupting data, skewing analytics, or rendering broken UI layouts.

Here is a breakdown of where unit conversion bugs hide in modern codebases, how they slip past tests, and how to eliminate them.

1. Time Duration Confusion: Seconds vs. Milliseconds

The single most common unit bug in distributed systems is duration ambiguity. Consider this seemingly harmless API response:

{
  "cache_ttl": 3600,
  "retry_interval": 5
}
Enter fullscreen mode Exit fullscreen mode

Is retry_interval 5 seconds or 5 milliseconds? If Service A sends 5 intending seconds, but Service B interprets 5 in setTimeout(fn, interval) as milliseconds, your retry logic will execute 200 times per second, DDOSing your own database.

In JavaScript:

// Danger: Ambiguous parameter units
function fetchWithTimeout(url, timeout) {
  // If caller passes 10 thinking seconds, timeout fires in 10ms!
  return Promise.race([
    fetch(url),
    new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), timeout))
  ]);
}
Enter fullscreen mode Exit fullscreen mode

Fix: Standardize variable names to include the unit suffix explicitly: retryIntervalMs, cacheTtlSeconds.

2. Binary vs. Decimal Storage Units (MB vs. MiB)

Is a megabyte 1,000,000 bytes or 1,048,576 bytes?

If your cloud storage provider bills and limits based on mebibytes (1 MiB = 2^20 = 1,048,576 bytes) but your frontend file upload validator uses decimal megabytes (1 MB = 10^6 = 1,000,000 bytes), users uploading a 10.4 MB file will pass client validation but get rejected by the server payload limit.

// Common mistake: mixing 1000 and 1024
const MAX_FILE_SIZE_DECIMAL = 10 * 1000 * 1000; // 10 MB
const MAX_FILE_SIZE_BINARY = 10 * 1024 * 1024;   // 10 MiB (~10.48 MB)
Enter fullscreen mode Exit fullscreen mode

3. Floating-Point Precision and Unit Rounding Errors

Converting between imperial and metric units involves non-terminating floating-point numbers. For example, 1 pound = 0.45359237 kilograms.

If you convert units back and forth during state transitions or API requests:

let weightLbs = 150;
let weightKg = weightLbs * 0.45359237; // 68.0388555
let recalculatedLbs = weightKg / 0.45359237; // 150.00000000000003
Enter fullscreen mode Exit fullscreen mode

Accumulated precision drift across repeated conversions leads to failed database equality assertions (recalculatedLbs !== 150) or subtle visual glitches in data tables.

When debugging multi-unit conversion routines or auditing incoming payload values during local testing, running quick sanity checks against a fast browser-based utility like the Nutilz Unit Converter lets you verify expected outputs without having to write temporary node scripts.

How to Eliminate Unit Conversion Bugs in Production

  1. Use Branded Types in TypeScript: Prevent passing raw numbers into functions expecting specific units:
   type Milliseconds = number & { readonly __brand: unique symbol };
   type Seconds = number & { readonly __brand: unique symbol };

   const toMs = (s: Seconds): Milliseconds => (s * 1000) as Milliseconds;
Enter fullscreen mode Exit fullscreen mode
  1. Store Canonical Units in Data Stores: Always store timestamps in UTC Unix epoch milliseconds and file sizes in raw bytes. Perform unit conversions only at display boundaries.
  2. Explicit API Contracts: Name JSON properties with unit suffixes (duration_ms, weight_kg, size_bytes) or use ISO 8601 strings (PT5S) for durations.

Conclusion

Unit conversion bugs are rarely loud, but their financial and technical cost is high. By embedding unit names into variable identifiers, enforcing typed unit boundaries, and validating edge conversions with tools like the nutilz.com unit converter, you can keep silent conversion errors out of your production deployments.

Top comments (0)