Add next calculator batch incl molarity and SWG

This commit is contained in:
Codex Agent
2026-03-08 02:19:18 +00:00
parent 5e974ea9c7
commit 1e341f051f
4 changed files with 161 additions and 43 deletions

View File

@@ -290,6 +290,100 @@ export function solve(
}
break;
}
case 'molarity': {
const m = v1; // mol/L
const gpl = v2; // grams/L
const molarMass = v3; // g/mol
if (source === 1) {
out.val2 = (!isNaN(m) && !isNaN(molarMass)) ? fmt(m * molarMass) : '';
} else if (source === 2) {
out.val1 = (!isNaN(gpl) && !isNaN(molarMass) && molarMass !== 0) ? fmt(gpl / molarMass) : '';
} else {
if (!isNaN(m) && !isNaN(molarMass)) out.val2 = fmt(m * molarMass);
else if (!isNaN(gpl) && !isNaN(molarMass) && molarMass !== 0) out.val1 = fmt(gpl / molarMass);
}
break;
}
case 'rockwell-vickers': {
const hrcToBhn = (h: number) => (1520000 - 4500 * h) / Math.pow(100 - h, 2);
const bhnToHrc = (b: number) => {
const disc = 4500 ** 2 + 4 * b * 1070000;
const y = (4500 + Math.sqrt(disc)) / (2 * b);
return 100 - y;
};
const bhnToHv = (b: number) => b * 0.95;
const hvToBhn = (hv: number) => hv / 0.95;
if (source === 1) {
const hrc = v1;
if (!isNaN(hrc) && hrc < 100) {
const hv = bhnToHv(hrcToBhn(hrc));
out.val2 = fmt(hv);
} else out.val2 = '';
} else {
const hv = v2;
if (!isNaN(hv) && hv > 0) {
const bhn = hvToBhn(hv);
out.val1 = fmt(bhnToHrc(bhn));
} else out.val1 = '';
}
break;
}
case 'sus-cst': {
const susToCst = (sus: number) => {
if (sus <= 0) return NaN;
if (sus < 100) return 0.226 * sus - 195 / sus;
return 0.22 * sus - 135 / sus;
};
const cstToSus = (cst: number) => {
if (cst <= 0) return NaN;
const low = (cst + Math.sqrt(cst * cst + 4 * 0.226 * 195)) / (2 * 0.226);
const high = (cst + Math.sqrt(cst * cst + 4 * 0.22 * 135)) / (2 * 0.22);
return low < 100 ? low : high;
};
if (source === 1) {
out.val2 = !isNaN(v1) ? fmt(susToCst(v1)) : '';
} else {
out.val1 = !isNaN(v2) ? fmt(cstToSus(v2)) : '';
}
break;
}
case 'swg': {
const swgTable: Record<number, number> = {
0: 8.23, 1: 7.62, 2: 7.01, 3: 6.4, 4: 5.89, 5: 5.38, 6: 4.88, 7: 4.47,
8: 4.06, 9: 3.66, 10: 3.25, 11: 2.95, 12: 2.64, 13: 2.34, 14: 2.03, 15: 1.83,
16: 1.63, 17: 1.42, 18: 1.22, 19: 1.02, 20: 0.91, 21: 0.81, 22: 0.71, 23: 0.61,
24: 0.56, 25: 0.51, 26: 0.46, 27: 0.42, 28: 0.38, 29: 0.35, 30: 0.32, 31: 0.29,
32: 0.27, 33: 0.25, 34: 0.23, 35: 0.21, 36: 0.19, 37: 0.17, 38: 0.15, 39: 0.14,
40: 0.12, 41: 0.11, 42: 0.1, 43: 0.09, 44: 0.08, 45: 0.07, 46: 0.064, 47: 0.058,
48: 0.051, 49: 0.045, 50: 0.04
};
const gaugeToMm = (g: number) => swgTable[Math.round(g)];
const mmToGauge = (mm: number) => {
let best = -1, bestDiff = Infinity;
for (const [gStr, diam] of Object.entries(swgTable)) {
const diff = Math.abs(mm - diam);
if (diff < bestDiff) { bestDiff = diff; best = parseInt(gStr, 10); }
}
return best;
};
if (calc.labels.in1.toLowerCase().includes('swg')) {
if (source === 1) out.val2 = !isNaN(v1) ? fmt(gaugeToMm(v1) ?? NaN) : '';
else out.val1 = (!isNaN(v2) && v2 > 0) ? fmt(mmToGauge(v2)) : '';
} else {
if (source === 1) out.val2 = (!isNaN(v1) && v1 > 0) ? fmt(mmToGauge(v1)) : '';
else out.val1 = !isNaN(v2) ? fmt(gaugeToMm(v2) ?? NaN) : '';
}
break;
}
}
return out;