Resources Numerical accuracy Calculation policy

Typed scale factors and shrinkage allowance

Reads a scale factor a person types, and gives the pattern factor for a casting that shrinks as it cools.

How the precision is kept

Factors are parsed from text with Number and combined with one division or addition in 64-bit numbers. The shrinkage factor has two stated modes.

Calculations under this policy

2 calculations in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.

scale-factors.js ModelCraft 0.8.1 64-bit 2 calculations Reviewed

Tests tools/modelcraft-test/scale08.mjs

Factor from text

64-bit

A factor from a plain number, a percent of the size, or a percent change.

f = n; n ÷ 100; 1 ± n ÷ 100

The text is read with Number, a decimal comma is accepted, and the result is divided or added once. A factor at or below 1e-6, at or above 1e6, or not a number gives null.

  • scale-factors.js
  • lines 14 to 26
  • ModelCraft 0.8.1
/** A factor from text: a plain number, "101.5%" (of the size), or "+1.5%" / "-2%" (change). */
export function parseFactor(text) {
  const s = String(text ?? '').trim().replace(',', '.').replace(/\s+/g, '');
  if (!s) return null;
  const m = /^([+-]?)(\d*\.?\d+(?:e[+-]?\d+)?)(%?)$/i.exec(s);
  if (!m) return null;
  const n = Number(m[2]);
  if (!Number.isFinite(n)) return null;
  let f;
  if (m[3]) f = m[1] ? 1 + (m[1] === '-' ? -n : n) / 100 : n / 100;
  else f = m[1] === '-' ? -n : n;
  return f > 1e-6 && f < 1e6 ? f : null;
}

Shrinkage allowance

64-bit

The pattern factor for a casting that shrinks by a percentage.

add: 1 + s ÷ 100; exact: 1 ÷ (1 − s ÷ 100)

One 64-bit expression. The default 'add' mode is the usual shrink rule and differs from the exact reciprocal; for s = 2 percent the two are 1.02 and 1.0204.

  • scale-factors.js
  • lines 38 to 42
  • ModelCraft 0.8.1
/** The pattern factor for a shrinkage of pct percent (0 ≤ pct < 50). */
export function shrinkFactor(pct, mode = 'add') {
  if (!(pct >= 0 && pct < 50)) return null;
  return mode === 'exact' ? 1 / (1 - pct / 100) : 1 + pct / 100;
}

In detail

Method
Percent forms: 101.5% is 1.015; +1.5% is 1 + 1.5/100. Shrinkage 'add' mode: 1 + s/100; 'exact' mode: 1 ÷ (1 − s/100).
Number types
64-bit numbers.
Tolerances
A factor must be between 1e-6 and 1e6 (exclusive); shrinkage must be at least 0 and under 50 percent.
Limits
The default mode is 'add', which is the rule of adding the shrinkage percent; 'exact' gives a slightly larger factor.

Approach: Exact values

Constants and factors are held as exact fractions from their definitions. The arithmetic is ordered so that no rounded factor is ever used.

Every policy that takes this approach