manufacturing.tolerance-check
Check a measurement against a nominal size and its upper and lower deviations, in whole micrometres.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 14 tests, run in TypeScript, Python and Rust.
What it does
Checks one measurement against a nominal size and its tolerance, the way a go/no-go gauge or an inspection sheet does:
lowerLimit = nominal + lowerDeviation upperLimit = nominal + upperDeviation passed = lowerLimit <= measured <= upperLimit
For example
checkTolerance(25,030, 25,000, 50, -50)→ passed true, status within, deviation 30, lower limit 24,950, upper limit 25,050, out by 0 symmetric ±0.05 mm at 25 mm: 25.030 passescheckTolerance(25,050, 25,000, 50, -50)→ passed true, status within, deviation 50, lower limit 24,950, upper limit 25,050, out by 0 exactly on the upper limit passes: limits are inclusivecheckTolerance(25,051, 25,000, 50, -50)→ passed false, status above, deviation 51, lower limit 24,950, upper limit 25,050, out by 1 one micrometre over the upper limit is a reject
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
export function checkTolerance(measured: number, nominal: number, upperDeviation: number, lowerDeviation: number): ToleranceCheck
| measured | int | the measured size in micrometres (1 mm = 1000) |
| nominal | int | the nominal (basic) size in micrometres |
| upperDeviation | int | upper limit minus nominal: +50 for ±0.05 mm, -7 for a g6 shaft at 25 mm |
| lowerDeviation | int | lower limit minus nominal: -50 for ±0.05 mm, -20 for a g6 shaft at 25 mm |
| returns | ToleranceCheck |
The types it declares, generated into your project
/** The verdict, where the limits are, and how far out a reject is. */
export interface ToleranceCheck {
/** true when lowerLimit <= measured <= upperLimit; the limits are inside */
readonly passed: boolean;
readonly status: ToleranceStatus;
/** measured minus nominal, signed */
readonly deviation: number;
readonly lowerLimit: number;
readonly upperLimit: number;
/** distance past the limit that was broken; 0 when passed */
readonly outBy: number;
}
export type ToleranceStatus = "within" | "above" | "below";
Your code names it in one line, in the file that uses it
import { checkTolerance } from "#fune/manufacturing.tolerance-check@^1";
import { type ToleranceCheck } from "./manufacturing_tolerance_check_types.ts";
const LIMIT = 1_000_000_000_000;
function micrometres(name: string, value: number): number {
if (!Number.isInteger(value) || value > LIMIT || value < -LIMIT) {
throw new RangeError(`${name} must be a whole number of micrometres within ±10^12, received ${value}`);
}
return value;
}
/**
* Check a measurement against nominal plus its upper and lower deviations.
*
* The deviations are signed and independent, so a tolerance band that lies
* wholly above or below nominal (an H7 hole, a g6 shaft) is checked correctly;
* a symmetric band is just `+t, -t`.
*/
export function checkTolerance(measured: number, nominal: number, upperDeviation: number, lowerDeviation: number): ToleranceCheck {
micrometres("measured", measured);
micrometres("nominal", nominal);
micrometres("upperDeviation", upperDeviation);
micrometres("lowerDeviation", lowerDeviation);
if (lowerDeviation > upperDeviation) {
throw new RangeError(
`lowerDeviation must not be greater than upperDeviation, received ${lowerDeviation} and ${upperDeviation}`,
);
}
const lowerLimit = nominal + lowerDeviation;
const upperLimit = nominal + upperDeviation;
const deviation = measured - nominal;
if (measured > upperLimit) {
return { passed: false, status: "above", deviation, lowerLimit, upperLimit, outBy: measured - upperLimit };
}
if (measured < lowerLimit) {
return { passed: false, status: "below", deviation, lowerLimit, upperLimit, outBy: lowerLimit - measured };
}
return { passed: true, status: "within", deviation, lowerLimit, upperLimit, outBy: 0 };
}Install
fune build
With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add manufacturing.tolerance-check
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./manufacturing.tolerance-check-1.0.0-typescript.fune, or fetch it from a terminal with fune pull manufacturing.tolerance-check@1.0.0:typescript.
The whole function, every language, is one file too: manufacturing.tolerance-check-1.0.0.fune, 14,651 bytes, sha256 02a3faf1c5fd1fc72dfdfa4fe659af7e86632dfe9b9993836204e86a78d84a43. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before manufacturing.tolerance-check
after — your function gets the result and the arguments, and returns the final result.
// fune: after manufacturing.tolerance-check
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show manufacturing.tolerance-check --steps.
// fune: step manufacturing.tolerance-check after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| symmetric ±0.05 mm at 25 mm: 25.030 passes | 25,030, 25,000, 50, -50 | → | passed true, status within, deviation 30, lower limit 24,950, upper limit 25,050, out by 0 |
| exactly on the upper limit passes: limits are inclusive | 25,050, 25,000, 50, -50 | → | passed true, status within, deviation 50, lower limit 24,950, upper limit 25,050, out by 0 |
| one micrometre over the upper limit is a reject | 25,051, 25,000, 50, -50 | → | passed false, status above, deviation 51, lower limit 24,950, upper limit 25,050, out by 1 |
| exactly on the lower limit passes | 24,950, 25,000, 50, -50 | → | passed true, status within, deviation -50, lower limit 24,950, upper limit 25,050, out by 0 |
| under the lower limit is a reject by the distance below it | 24,930, 25,000, 50, -50 | → | passed false, status below, deviation -70, lower limit 24,950, upper limit 25,050, out by 20 |
| a g6 shaft at 25 mm measured at exactly nominal is too big: both limits are below nominal | 25,000, 25,000, -7, -20 | → | passed false, status above, deviation 0, lower limit 24,980, upper limit 24,993, out by 7 |
| a g6 shaft at 24.990 mm is inside 24.980 to 24.993 | 24,990, 25,000, -7, -20 | → | passed true, status within, deviation -10, lower limit 24,980, upper limit 24,993, out by 0 |
| an H7 hole at 25 mm: nominal itself is the lower limit and passes | 25,000, 25,000, 21, 0 | → | passed true, status within, deviation 0, lower limit 25,000, upper limit 25,021, out by 0 |
| an H7 hole one micrometre under nominal is a reject | 24,999, 25,000, 21, 0 | → | passed false, status below, deviation -1, lower limit 25,000, upper limit 25,021, out by 1 |
| a zero tolerance accepts only the exact size | 10,000, 10,000, 0, 0 | → | passed true, status within, deviation 0, lower limit 10,000, upper limit 10,000, out by 0 |
Show the other 4 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| a negative coordinate from a datum, -12.000 mm +0.1/-0.1, measured -12.150 | -12,150, -12,000, 100, -100 | → | passed false, status below, deviation -150, lower limit -12,100, upper limit -11,900, out by 50 |
| a lower deviation above the upper one is an error | 25,000, 25,000, -20, -7 | → | error: lowerDeviation must not be greater than upperDeviation |
| a fractional micrometre is an error | 25,000.5, 25,000, 50, -50 | → | error: measured must be a whole number of micrometres |
| a size beyond 10^12 micrometres is an error | 25,000, 1,000,000,000,001, 50, -50 | → | error: nominal must be a whole number of micrometres within ±10^12 |
More from the author
**Deviations, not a ± number.** Tolerances are given as the upper and lower deviation from nominal, as ISO 286 writes them (`es`/`ei` for shafts, `ES`/`EI` for holes), so both kinds of limit are one call:
- symmetric, 25 mm ± 0.05: `checkTolerance(m, 25000, 50, -50)` - asymmetric, a 25 mm H7 hole (+0.021 / 0): `checkTolerance(m, 25000, 21, 0)` - one-sided, a 25 mm g6 shaft (-0.007 / -0.020): `checkTolerance(m, 25000, -7, -20)`
The last is the case a "within ±tolerance of nominal" check gets wrong: a g6 shaft measured at exactly 25.000 mm is **too big**, 7 µm above its upper limit, because both limits sit below nominal.
**Whole micrometres.** Every size is an integer number of micrometres (µm), so limits and deviations are exact and all three languages agree; a reading of 25.0125 mm is 25013 µm after the gauge's own rounding. Every value must lie within ±10^12 µm (1,000 km), which keeps every sum exact.
**Limits are inclusive.** A part measured exactly on a limit passes. `outBy` is how far past the broken limit a reject is (always positive for a reject, 0 for a pass), and `status` says which side: `above`, `below` or `within`.
The lower deviation may not be greater than the upper one. Negative sizes are allowed, for coordinates and offsets measured from a datum.
Source: ISO 286-1:2010, Geometrical product specifications (GPS) - ISO code system for tolerances on linear sizes, clause 3 (deviations and limits). The H7 and g6 values for 25 mm are from its tables (IT6 = 13 µm, IT7 = 21 µm, fundamental deviation g = -7 µm for sizes over 18 up to 30 mm).
Files
| Path | Bytes |
|---|---|
| README.md | 1,882 |
| impl/python.py | 1,894 |
| impl/rust.rs | 2,815 |
| impl/typescript.ts | 1,671 |
| vectors.json | 3,014 |