roundHalfUp function
mm in whole quanta of quantumMm, round-half-up, the half decided
robustly (spec 11 D9, R-13): with x = mm / quantum and n = floor(x),
a value within dimFormat.linear mm of (n + 0.5) ยท quantum is on the
half and gives n + 1; any other value gives round(x).
The half is decided in millimetres, the unit the geometry has. A length
meant to end on a half arrives as a computed double a few ulps off it,
below it about half the time, and an imperial half has no exact double
(3/16" is 4.762499999999999 mm), so neither the naive round nor exact
arithmetic on the stored double decides those halves as intended (the
spike's Q5c). The cost, recorded in D18 and pinned by DF2: a true
length within 1e-6 mm below a half prints rounded up (R-32).
Implementation
int roundHalfUp(double mm, double quantumMm) {
final x = mm / quantumMm;
final n = x.floorToDouble();
if ((mm - (n + 0.5) * quantumMm).abs() <= dimFormat.linear) {
return n.toInt() + 1;
}
return x.round();
}