'use strict'; // power-conversion.cjs -- METHOD DECK (X063), reversible power unit conversion. // Factors are exact watt-equivalents from NIST SP 811 (2008 ed.), Appendix B.8 "Power". 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); } // Mechanical horsepower = 550 ft*lbf/s, built exactly from ft = 0.3048 m and // lbf = 0.45359237 kg * 9.80665 m/s2 (all exact NIST SP 811 values). const HP_MECH_W = 550 * 0.3048 * (0.45359237 * 9.80665); // Metric horsepower = 75 kgf*m/s = 75 kg * standard gravity, exact. const HP_METRIC_W = 75 * 9.80665; // BTU/h from the exact BTU (IT) = 1055.05585262 J over one hour (3600 s, exact). const BTU_H_W = 1055.05585262 / 3600; const TO_W = { W: 1, kW: 1000, hp_mech: HP_MECH_W, hp_metric: HP_METRIC_W, btu_h: BTU_H_W, }; const UNITS = Object.keys(TO_W); const DISPLAY = { W: 'W', kW: 'kW', hp_mech: 'hp (mechanical)', hp_metric: 'hp (metric)', btu_h: 'BTU/h' }; module.exports = { slug: 'power-conversion', title: 'Power conversion', deck_category: 'units', description: 'Converts power between W, kW, mechanical hp, metric hp, and BTU/h using exact NIST SP 811 watt-equivalents.', inputs: [ { name: 'value', type: 'number', unit: 'from_unit', required: true, description: 'The power 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 power' } ], steps: [ 'Look up the exact watt-equivalent of from_unit and of to_unit.', 'Convert the input to watts: watts = value * w_per(from_unit).', 'Convert watts to the target unit: result = watts / w_per(to_unit).', 'Round result to 6 significant figures.', ], constants: [ { name: 'W', value: 1, unit: 'W', source: 0 }, { name: 'kW', value: 1000, unit: 'W', source: 0 }, { name: 'hp_mech', value: HP_MECH_W, unit: 'W', source: 0 }, { name: 'hp_metric', value: HP_METRIC_W, unit: 'W', source: 0 }, { name: 'btu_h', value: BTU_H_W, unit: 'W', source: 0 }, ], sources: [ { kind: 'standard', value: 'NIST Special Publication 811 (2008 edition), Appendix B.8, Table for power', 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: 'Mechanical (imperial) horsepower = 550 ft*lbf/s built from the exact foot and pound-force; metric horsepower = 75 kgf*m/s = 75 * 9.80665 W exactly; BTU/h uses the exact BTU (IT) over one hour.' }, ], disclaimer: null, questionText(inputs) { return `What is ${inputs.value} ${DISPLAY[inputs.from_unit]} in ${DISPLAY[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_W.hasOwnProperty(from_unit)) throw new Error('from_unit must be one of: ' + UNITS.join(', ')); if (!TO_W.hasOwnProperty(to_unit)) throw new Error('to_unit must be one of: ' + UNITS.join(', ')); const w = value * TO_W[from_unit]; const result = w / TO_W[to_unit]; return { answer: `${fmtSig(result, 6)} ${DISPLAY[to_unit]}`, values: { result } }; }, examples: [ { name: 'worked example', inputs: { value: 1, from_unit: 'hp_mech', to_unit: 'W' }, expected: { answer: '745.700 W' }, check: 'By hand: 550 * 0.3048 m * (0.45359237 kg * 9.80665 m/s2) = 745.69987... W; rounded to 6 significant figures is 745.700.' }, { name: 'test vector: metric hp to watts', inputs: { value: 1, from_unit: 'hp_metric', to_unit: 'W' }, expected: { answer: '735.499 W' }, check: 'By hand: 75 kg * 9.80665 m/s2 = 735.49875 W exactly, rounded to 6 significant figures.' }, { name: 'test vector: kW to BTU/h', inputs: { value: 1, from_unit: 'kW', to_unit: 'btu_h' }, expected: { answer: '3,412.14 BTU/h' }, check: 'By hand: 1000 W / (1055.05585262/3600 W) = 3412.1416...; rounded to 6 significant figures is 3412.14.' }, ], };