'use strict'; // pressure-conversion.cjs -- METHOD DECK (X063), reversible pressure unit conversion. // Factors are exact pascal-equivalents from NIST SP 811 (2008 ed.), Appendix B.9 "Pressure". 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); } // psi: exact pound-force (0.45359237 kg * 9.80665 m/s2, both exact) per exact square inch (0.0254 m)^2. const PSI_PA = (0.45359237 * 9.80665) / (0.0254 * 0.0254); // Conventional mercury density 13595.1 kg/m3 * standard gravity 9.80665 m/s2 * column height. const MMHG_PA = 13595.1 * 9.80665 * 0.001; const INHG_PA = 13595.1 * 9.80665 * 0.0254; // Pascals per 1 unit. const TO_PA = { Pa: 1, kPa: 1000, bar: 100000, atm: 101325, psi: PSI_PA, mmHg: MMHG_PA, inHg: INHG_PA, }; const UNITS = Object.keys(TO_PA); module.exports = { slug: 'pressure-conversion', title: 'Pressure conversion', deck_category: 'units', description: 'Converts a pressure between Pa, kPa, bar, atm, psi, mmHg, and inHg using exact NIST SP 811 pascal-equivalents.', inputs: [ { name: 'value', type: 'number', unit: 'from_unit', required: true, description: 'The pressure 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 pressure' } ], steps: [ 'Look up the exact pascal-equivalent of from_unit and of to_unit.', 'Convert the input to pascals: pa = value * pa_per(from_unit).', 'Convert pascals to the target unit: result = pa / pa_per(to_unit).', 'Round result to 6 significant figures.', ], constants: [ { name: 'Pa', value: 1, unit: 'Pa', source: 0 }, { name: 'kPa', value: 1000, unit: 'Pa', source: 0 }, { name: 'bar', value: 100000, unit: 'Pa', source: 0 }, { name: 'atm', value: 101325, unit: 'Pa', source: 0 }, { name: 'psi', value: PSI_PA, unit: 'Pa', source: 0 }, { name: 'mmHg', value: MMHG_PA, unit: 'Pa', source: 0 }, { name: 'inHg', value: INHG_PA, unit: 'Pa', source: 0 }, ], sources: [ { kind: 'standard', value: 'NIST Special Publication 811 (2008 edition), Appendix B.9, Table for pressure', 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: 'bar and standard atmosphere (atm) are exact by definition; psi is derived exactly from the pound-force and the square inch; mmHg and inHg use the conventional mercury density 13,595.1 kg/m3 at standard gravity 9.80665 m/s2.' }, ], disclaimer: null, questionText(inputs) { return `What 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_PA.hasOwnProperty(from_unit)) throw new Error('from_unit must be one of: ' + UNITS.join(', ')); if (!TO_PA.hasOwnProperty(to_unit)) throw new Error('to_unit must be one of: ' + UNITS.join(', ')); const pa = value * TO_PA[from_unit]; const result = pa / TO_PA[to_unit]; return { answer: `${fmtSig(result, 6)} ${to_unit}`, values: { result } }; }, examples: [ { name: 'worked example', inputs: { value: 1, from_unit: 'atm', to_unit: 'psi' }, expected: { answer: '14.6959 psi' }, check: 'By hand: 101325 Pa / 6894.757293... Pa/psi = 14.69595...; rounded to 6 significant figures is 14.6959.' }, { name: 'test vector: bar to kPa', inputs: { value: 1, from_unit: 'bar', to_unit: 'kPa' }, expected: { answer: '100.000 kPa' }, check: 'By definition 1 bar = 100,000 Pa = 100 kPa exactly.' }, { name: 'test vector: inHg to mmHg', inputs: { value: 1, from_unit: 'inHg', to_unit: 'mmHg' }, expected: { answer: '25.4000 mmHg' }, check: 'By hand: both use the same mercury column density and gravity, so the ratio is exactly the length ratio: 1 inch / 1 mm = 25.4.' }, ], };