"""Body fat percentage: the U.S. Navy circumference method (Hodgdon and Beckett 1984, metric form, Siri 1961 density to fat), fat and lean mass, the ACE category, and the CUN-BAE estimate (Gomez-Ambrosi et al. 2012) from BMI, age and sex as a cross-check. Inputs: sex (male or female, default male), height_cm (30 to 275, default 175), neck_cm (10 to 100, default 38), waist_cm (30 to 250, default 85), hip_cm (30 to 250, default 96, women only), weight_kg (1 to 650, default 80), age (1 to 120, default 30, CUN-BAE only). Returns: bodyFatPercent (Navy, percent), fatMassKg, leanMassKg, category (ACE), cunBae (percent, as the equation gives it). mysi, Body fat percentage, mysi.dev/body-fat-percentage, version 1, changed 2026-12-04. MIT. """ import math def _body_fat_percentage_num(name, value, low, high): if isinstance(value, bool) or not isinstance(value, (int, float)) or not low <= value <= high: raise ValueError(f"{name} must be a number from {low} to {high}") return float(value) def body_fat_percentage(sex="male", height_cm=175, neck_cm=38, waist_cm=85, hip_cm=96, weight_kg=80, age=30): if sex not in ("male", "female"): raise ValueError("sex must be one of: male, female") h = _body_fat_percentage_num("height_cm", height_cm, 30, 275) neck = _body_fat_percentage_num("neck_cm", neck_cm, 10, 100) waist = _body_fat_percentage_num("waist_cm", waist_cm, 30, 250) hip = _body_fat_percentage_num("hip_cm", hip_cm, 30, 250) w = _body_fat_percentage_num("weight_kg", weight_kg, 1, 650) a = _body_fat_percentage_num("age", age, 1, 120) # Navy body density, centimetre constants (Hodgdon and Beckett 1984), then Siri 1961 if sex == "male": if waist <= neck: raise ValueError("waist_cm must be larger than neck_cm for men") density = 1.0324 - 0.19077 * math.log10(waist - neck) + 0.15456 * math.log10(h) else: if waist + hip <= neck: raise ValueError("waist_cm + hip_cm must be larger than neck_cm") density = 1.29579 - 0.35004 * math.log10(waist + hip - neck) + 0.22100 * math.log10(h) bf = 495 / density - 450 # CUN-BAE (Gomez-Ambrosi et al. 2012, Diabetes Care 35:383), sex 0 = male, 1 = female bmi = w / (h / 100) ** 2 s = 0 if sex == "male" else 1 cun = (-44.988 + 0.503 * a + 10.689 * s + 3.172 * bmi - 0.026 * bmi ** 2 + 0.181 * bmi * s - 0.02 * bmi * a - 0.005 * bmi ** 2 * s + 0.00021 * bmi ** 2 * a) # ACE body fat categories: lower edges of athletes, fitness, average, obese edges = (6, 14, 18, 25) if sex == "male" else (14, 21, 25, 32) names = ("Essential fat", "Athletes", "Fitness", "Average", "Obese") return { "bodyFatPercent": bf, "fatMassKg": w * bf / 100, "leanMassKg": w - w * bf / 100, "category": names[sum(bf >= e for e in edges)], "cunBae": cun, } BODY_FAT_PERCENTAGE_CASES = [ ({"sex": "female", "height_cm": 165, "neck_cm": 33, "waist_cm": 74, "hip_cm": 100, "weight_kg": 63, "age": 34}, {"bodyFatPercent": 28.9359, "fatMassKg": 18.2296}), ({"sex": "male", "height_cm": 180, "neck_cm": 40, "waist_cm": 90, "hip_cm": 100, "weight_kg": 85, "age": 40}, {"bodyFatPercent": 18.3675, "cunBae": 25.2472}), ({"sex": "female", "height_cm": 158, "neck_cm": 35, "waist_cm": 88, "hip_cm": 108, "weight_kg": 74, "age": 52}, {"bodyFatPercent": 40.4798, "leanMassKg": 44.0450}), ] def body_fat_percentage_check(tolerance=1e-3): for given, want in BODY_FAT_PERCENTAGE_CASES: got = body_fat_percentage(**given) for name, value in want.items(): close = abs(got[name] - value) <= tolerance * max(1, abs(value)) assert close, (name, got[name], value) return "ok" if __name__ == "__main__": import json, sys body_fat_percentage_check() given = dict(a.split("=", 1) for a in sys.argv[1:] if "=" in a) js = lambda v: v.lstrip("-")[:1].isdigit() or v[:1] in '[{".' or v in ("true", "false", "null") given = {k: json.loads(v) if js(v) else v for k, v in given.items()} if given: print(json.dumps(body_fat_percentage(**given)))