DEV Community

EtsyOptima
EtsyOptima

Posted on

Implementing Cockcroft-Gault in Code: Units, Weight Choice and Test Cases

The Cockcroft-Gault equation looks like a one-liner, which is exactly why it gets implemented carelessly. The arithmetic is trivial. The bugs live in the inputs: creatinine units, which body weight to use, and missing range checks. Here is how I would build and sanity-check it.

Not medical advice. This is a programming walkthrough. Clinical decisions belong to qualified professionals using current prescribing information and local protocols.

The formula

CrCl (mL/min) = ((140 − age) × weight in kg) / (72 × serum creatinine in mg/dL), multiplied by 0.85 for females.

A minimal implementation

SCR_UMOL_PER_MGDL = 88.4

def ideal_body_weight(height_cm, female):
    # Devine: 50 kg (men) / 45.5 kg (women) + 2.3 kg per inch over 60 in
    return (45.5 if female else 50.0) + 2.3 * (height_cm / 2.54 - 60)

def dosing_weight(actual_kg, height_cm, female):
    ibw = ideal_body_weight(height_cm, female)
    bmi = actual_kg / (height_cm / 100) ** 2
    if actual_kg < ibw:
        return actual_kg, "actual"
    if bmi >= 30 or actual_kg > 1.2 * ibw:
        return ibw + 0.4 * (actual_kg - ibw), "adjusted"
    return ibw, "ideal"

def cockcroft_gault(age, weight_kg, scr, female, unit="mg/dL"):
    if unit == "umol/L":
        scr /= SCR_UMOL_PER_MGDL
    if not 18 <= age <= 120 or weight_kg <= 0 or scr <= 0:
        raise ValueError("input outside intended range")
    crcl = (140 - age) * weight_kg / (72 * scr)
    return crcl * 0.85 if female else crcl
Enter fullscreen mode Exit fullscreen mode

Three things that commonly go wrong

1. Units. Many labs outside the US report creatinine in µmol/L. Feed 106 µmol/L into a function expecting mg/dL and you get a value almost 90 times too low. Convert explicitly (1 mg/dL = 88.4 µmol/L) and make the unit a required, visible parameter in your UI.

2. Weight basis. The equation does not say which weight to use. Common convention: actual weight if below ideal, ideal weight at normal BMI, adjusted weight (IBW + 0.4 × excess) in obesity. Return the basis alongside the weight so the user can see the choice, and treat it as configurable because institutional protocols differ.

3. Silent nonsense. A negative creatinine or an age of 3 should raise an error, not return a number. Children need different equations entirely.

Test against a known worked example

Take a 55-year-old man, 120 kg, 175 cm, creatinine 1.2 mg/dL. Ideal weight is about 70.5 kg, BMI is 39.2, so adjusted weight applies: about 90.3 kg. CrCl comes out at about 88.8 mL/min. Using actual weight would give about 118.1, a large overestimate.

w, basis = dosing_weight(120, 175, female=False)
assert basis == "adjusted" and round(w, 1) == 90.3
assert round(cockcroft_gault(55, w, 1.2, False), 1) == 88.8
# both unit paths must agree
assert abs(cockcroft_gault(55, w, 1.2, False)
           - cockcroft_gault(55, w, 1.2 * 88.4, False, "umol/L")) < 1e-9
Enter fullscreen mode Exit fullscreen mode

These figures match the worked example on CrCl Calculator, which I use as a reference oracle: it shows actual, ideal and adjusted weight side by side and accepts mg/dL or µmol/L. Its ideal body weight calculator is handy for checking the Devine step on its own.

Keep rounding for display only, and show your working. Users trust a number they can follow.

Top comments (0)