"use strict"; function round2(x) { return Math.round(x * 100) / 100; } function heronArea(a, b, c) { const s = (a + b + c) / 2; const inner = s * (s - a) * (s - b) * (s - c); if (inner <= 0) throw new Error("sides do not form a valid triangle"); return Math.sqrt(inner); } module.exports = { slug: "triangle-area-heron", title: "Triangle area by Heron's formula", deck_category: "geometry", description: "Computes the area of a triangle from its three side lengths using Heron's formula.", inputs: [ { name: "a", type: "number", unit: "length units", required: true, description: "First side length.", min: 0 }, { name: "b", type: "number", unit: "length units", required: true, description: "Second side length.", min: 0 }, { name: "c", type: "number", unit: "length units", required: true, description: "Third side length.", min: 0 } ], outputs: [ { name: "area", unit: "square length units", description: "Triangle area." } ], steps: [ "Compute the semiperimeter s = (a + b + c) / 2.", "Check the triangle inequality: s*(s-a)*(s-b)*(s-c) must be positive, otherwise the three lengths cannot form a triangle.", "area = sqrt( s*(s-a)*(s-b)*(s-c) ).", "Round to 2 decimals." ], constants: [], sources: [ { kind: "edition_page", value: "Coxeter, H.S.M. Introduction to Geometry, 2nd ed. Wiley, 1969, section 1.3.", title: "Introduction to Geometry, 2nd ed., Heron's formula", publisher: "Wiley", retrieved_utc: "2026-09-30T19:46:00Z", note: "Standard geometry reference for Heron's formula (attributed to Heron of Alexandria, Metrica)." } ], disclaimer: null, questionText(inputs) { return `What is the area of a triangle with sides ${inputs.a}, ${inputs.b}, and ${inputs.c}, using Heron's formula?`; }, run(inputs) { const { a, b, c } = inputs || {}; for (const [name, v] of Object.entries({ a, b, c })) { if (typeof v !== "number" || v <= 0) throw new Error(`${name} must be a positive number`); } const area = round2(heronArea(a, b, c)); return { answer: `${area.toFixed(2)}`, values: { area } }; }, examples: [ { name: "worked example", inputs: { a: 3, b: 4, c: 5 }, expected: { answer: "6.00" }, check: "A 3-4-5 triangle is a right triangle with legs 3 and 4, so its area is independently known to be (3*4)/2 = 6.00, matching Heron's formula's sqrt(6*3*2*1) = sqrt(36) = 6.00." }, { name: "test vector: isosceles triangle", inputs: { a: 5, b: 5, c: 6 }, expected: { answer: "12.00" }, check: "A 5-5-6 isosceles triangle splits into two 3-4-5 right triangles with base 6 and height 4, so its area is independently known to be (6*4)/2 = 12.00, matching Heron's formula's sqrt(8*3*3*2) = sqrt(144) = 12.00." } ] };