'use strict'; // energy-conversion.cjs -- METHOD DECK (X063), reversible energy unit conversion. // Factors are exact joule-equivalents from NIST SP 811 (2008 ed.), Appendix B.8 "Energy, work". function roundSig(x, sig) { if (x === 0) return { value: 0, decimals: 0 }; const neg = x < 0; const ax = Math.abs(x); let exp = Math.floor(Math.log10(ax)); let decimals = Math.max(sig - exp - 1, 0); let factor = Math.pow(10, decimals); let value = Math.round(ax * factor) / factor; const newExp = value === 0 ? exp : Math.floor(Math.log10(value)); if (newExp !== exp) { decimals = Math.max(sig - newExp - 1, 0); factor = Math.pow(10, decimals); value = Math.round(ax * factor) / factor; } return { value: neg ? -value : value, decimals }; } function fmt(x, decimals) { return x.toLocaleString('en-US', { minimumFractionDigits: decimals, maximumFractionDigits: decimals }); } function fmtSig(x, sig) { const r = roundSig(x, sig); return fmt(r.value, r.decimals); } // Joules per 1 unit. const TO_J = { J: 1, kJ: 1000, kWh: 3600000, cal: 4.184, kcal: 4184, BTU: 1055.05585262, Wh: 3600, }; const UNITS = Object.keys(TO_J); module.exports = { slug: 'energy-conversion', title: 'Energy conversion', deck_category: 'units', description: 'Converts an energy quantity between J, kJ, kWh, cal, kcal, BTU, and Wh using exact NIST SP 811 joule-equivalents.', inputs: [ { name: 'value', type: 'number', unit: 'from_unit', required: true, description: 'The energy to convert' }, { name: 'from_unit', type: 'string', required: true, description: 'Unit of the input value', enum: UNITS }, { name: 'to_unit', type: 'string', required: true, description: 'Unit to convert into', enum: UNITS }, ], outputs: [ { name: 'result', unit: 'to_unit', description: 'The converted energy' } ], steps: [ 'Look up the exact joule-equivalent of from_unit and of to_unit.', 'Convert the input to joules: joules = value * j_per(from_unit).', 'Convert joules to the target unit: result = joules / j_per(to_unit).', 'Round result to 6 significant figures.', ], constants: [ { name: 'J', value: 1, unit: 'J', source: 0 }, { name: 'kJ', value: 1000, unit: 'J', source: 0 }, { name: 'kWh', value: 3600000, unit: 'J', source: 0 }, { name: 'cal', value: 4.184, unit: 'J', source: 0 }, { name: 'kcal', value: 4184, unit: 'J', source: 0 }, { name: 'BTU', value: 1055.05585262, unit: 'J', source: 0 }, { name: 'Wh', value: 3600, unit: 'J', source: 0 }, ], sources: [ { kind: 'standard', value: 'NIST Special Publication 811 (2008 edition), Appendix B.8, Table for energy, work, quantity of heat', title: 'Guide for the Use of the International System of Units (SI)', publisher: 'National Institute of Standards and Technology', retrieved_utc: '2026-09-30T19:29:48Z', note: 'kWh and Wh are exact from the SI watt and hour (3600 s); cal is the exact thermochemical calorie (4.184 J); BTU is the exact International Table (IT) British thermal unit, 1055.05585262 J.' }, ], disclaimer: null, questionText(inputs) { return `How much is ${inputs.value} ${inputs.from_unit} in ${inputs.to_unit}?`; }, run(inputs) { const { value, from_unit, to_unit } = inputs || {}; if (typeof value !== 'number' || !isFinite(value)) throw new Error('value must be a finite number'); if (!TO_J.hasOwnProperty(from_unit)) throw new Error('from_unit must be one of: ' + UNITS.join(', ')); if (!TO_J.hasOwnProperty(to_unit)) throw new Error('to_unit must be one of: ' + UNITS.join(', ')); const j = value * TO_J[from_unit]; const result = j / TO_J[to_unit]; return { answer: `${fmtSig(result, 6)} ${to_unit}`, values: { result } }; }, examples: [ { name: 'worked example', inputs: { value: 1, from_unit: 'kWh', to_unit: 'kJ' }, expected: { answer: '3,600.00 kJ' }, check: 'By definition 1 kWh = 3,600,000 J = 3600 kJ exactly.' }, { name: 'test vector: kilocalories to joules', inputs: { value: 1, from_unit: 'kcal', to_unit: 'J' }, expected: { answer: '4,184.00 J' }, check: 'By definition 1 kcal = 1000 cal = 1000 * 4.184 J = 4184 J exactly.' }, { name: 'test vector: BTU to joules', inputs: { value: 1, from_unit: 'BTU', to_unit: 'J' }, expected: { answer: '1,055.06 J' }, check: 'By definition 1 BTU (IT) = 1055.05585262 J exactly; rounded to 6 significant figures is 1055.06.' }, ], };