"use strict"; function round2(x) { return Math.round(x * 100) / 100; } module.exports = { slug: "sphere-and-cylinder-volumes", title: "Sphere and cylinder volume and surface area", deck_category: "geometry", description: "Computes the volume and total surface area of a sphere or a right circular cylinder.", inputs: [ { name: "shape", type: "string", required: true, description: "Which solid to measure.", enum: ["sphere", "cylinder"] }, { name: "radius", type: "number", unit: "length units", required: true, description: "Radius of the sphere or cylinder.", min: 0 }, { name: "height", type: "number", unit: "length units", required: false, description: "Height of the cylinder (ignored for sphere).", min: 0 } ], outputs: [ { name: "volume", unit: "cubic length units", description: "Volume of the solid." }, { name: "surface_area", unit: "square length units", description: "Total surface area of the solid." } ], steps: [ "For a sphere of radius r: volume = (4/3) * pi * r^3; surface area = 4 * pi * r^2.", "For a right circular cylinder of radius r and height h: volume = pi * r^2 * h; total surface area = 2*pi*r*h + 2*pi*r^2 (the side plus both circular ends).", "Round both results to 2 decimals." ], constants: [ { name: "pi", value: 3.14159265358979, unit: "dimensionless", source: 0 } ], sources: [ { kind: "edition_page", value: "Standard geometry reference (e.g. Coxeter, H.S.M. Introduction to Geometry, 2nd ed., Wiley, 1969).", title: "Sphere and cylinder volume and surface area formulas", publisher: "Wiley", retrieved_utc: "2026-09-30T19:49:00Z", note: "Standard solid-geometry formulas." } ], disclaimer: null, questionText(inputs) { if (inputs.shape === "sphere") return `What are the volume and surface area of a sphere with radius ${inputs.radius}?`; return `What are the volume and surface area of a cylinder with radius ${inputs.radius} and height ${inputs.height}?`; }, run(inputs) { const { shape, radius, height } = inputs || {}; if (shape !== "sphere" && shape !== "cylinder") throw new Error("shape must be sphere or cylinder"); if (typeof radius !== "number" || radius <= 0) throw new Error("radius must be a positive number"); let volume, surface; if (shape === "sphere") { volume = (4 / 3) * Math.PI * radius ** 3; surface = 4 * Math.PI * radius ** 2; } else { if (typeof height !== "number" || height <= 0) throw new Error("height must be a positive number for a cylinder"); volume = Math.PI * radius ** 2 * height; surface = 2 * Math.PI * radius * height + 2 * Math.PI * radius ** 2; } volume = round2(volume); surface = round2(surface); return { answer: `volume ${volume.toFixed(2)}, surface area ${surface.toFixed(2)}`, values: { volume, surface_area: surface } }; }, examples: [ { name: "worked example", inputs: { shape: "sphere", radius: 3 }, expected: { answer: "volume 113.10, surface area 113.10" }, check: "By hand: volume=(4/3)*3.14159265*27=113.0973 rounds to 113.10; surface=4*3.14159265*9=113.0973 rounds to 113.10. It is an independently known fact that a sphere's volume and surface area are numerically equal precisely at radius 3, since (4/3)pi*r^3=4*pi*r^2 solves to r=3, giving a built-in cross-check that both values should match." }, { name: "test vector: cylinder", inputs: { shape: "cylinder", radius: 2, height: 5 }, expected: { answer: "volume 62.83, surface area 87.96" }, check: "By hand: volume=3.14159265*4*5=62.8319 rounds to 62.83; surface=2*3.14159265*2*5+2*3.14159265*4=62.8319+25.1327=87.9646 rounds to 87.96." } ] };