'use strict'; // mass-conversion.cjs -- METHOD DECK (X063), reversible mass unit conversion. // Factors are the exact kilogram-equivalents from NIST SP 811 (2008 ed.), Appendix B.8 "Mass". 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); } // Kilograms per 1 unit. All exact by definition (NIST SP 811 Appendix B.8). const TO_KG = { mg: 0.000001, g: 0.001, kg: 1, t: 1000, oz: 0.028349523125, lb: 0.45359237, st: 6.35029318, }; const UNITS = Object.keys(TO_KG); module.exports = { slug: 'mass-conversion', title: 'Mass conversion', deck_category: 'units', description: 'Converts a mass between metric and avoirdupois units using exact NIST SP 811 kilogram-equivalents.', inputs: [ { name: 'value', type: 'number', unit: 'from_unit', required: true, description: 'The mass 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 mass' } ], steps: [ 'Look up the exact kilogram-equivalent of from_unit and of to_unit from the NIST SP 811 table.', 'Convert the input to kilograms: kilograms = value * kg_per(from_unit).', 'Convert kilograms to the target unit: result = kilograms / kg_per(to_unit).', 'Round result to 6 significant figures.', ], constants: [ { name: 'mg', value: 0.000001, unit: 'kg', source: 0 }, { name: 'g', value: 0.001, unit: 'kg', source: 0 }, { name: 'kg', value: 1, unit: 'kg', source: 0 }, { name: 't', value: 1000, unit: 'kg', source: 0 }, { name: 'oz', value: 0.028349523125, unit: 'kg', source: 0 }, { name: 'lb', value: 0.45359237, unit: 'kg', source: 0 }, { name: 'st', value: 6.35029318, unit: 'kg', source: 0 }, ], sources: [ { kind: 'standard', value: 'NIST Special Publication 811 (2008 edition), Appendix B.8, Table for mass (avoirdupois)', 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: 'oz and lb are exact avoirdupois definitions; st (stone) = 14 lb exactly; t is the metric tonne (1000 kg exactly).' }, ], disclaimer: null, questionText(inputs) { return `How many ${inputs.to_unit} are in ${inputs.value} ${inputs.from_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_KG.hasOwnProperty(from_unit)) throw new Error('from_unit must be one of: ' + UNITS.join(', ')); if (!TO_KG.hasOwnProperty(to_unit)) throw new Error('to_unit must be one of: ' + UNITS.join(', ')); const kg = value * TO_KG[from_unit]; const result = kg / TO_KG[to_unit]; return { answer: `${fmtSig(result, 6)} ${to_unit}`, values: { result } }; }, examples: [ { name: 'worked example', inputs: { value: 10, from_unit: 'lb', to_unit: 'kg' }, expected: { answer: '4.53592 kg' }, check: 'By hand: 10 lb * 0.45359237 kg/lb = 4.5359237 kg, rounded to 6 significant figures is 4.53592.' }, { name: 'test vector: stone to pounds', inputs: { value: 1, from_unit: 'st', to_unit: 'lb' }, expected: { answer: '14.0000 lb' }, check: 'By definition 1 stone = 14 lb exactly: 6.35029318 kg / 0.45359237 kg/lb = 14 exactly.' }, { name: 'test vector: kilograms to ounces', inputs: { value: 5, from_unit: 'kg', to_unit: 'oz' }, expected: { answer: '176.370 oz' }, check: 'By hand: 5 kg / 0.028349523125 kg/oz = 176.3698...; rounded to 6 significant figures is 176.370.' }, ], };