Resources Numerical accuracy Calculation policy

I/O channel counts with spare allowance

Counts the control system channels of each type on a P&ID and adds the spare allowance.

How the precision is kept

Channels are counted as integers per type. The spare allowance multiplies each count by 1 + spare in 64-bit and rounds up. Because 1 + spare is a binary fraction, a product that should be a whole number can land just above it and round up one more.

Open item. Compute the spare allowance in whole numbers, for example ⌈count × (100 + percent) ÷ 100⌉ with the percent as an integer, so an exact product is not rounded up by one.

Calculations under this policy

1 calculation 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.

process/controls.js CadCraft 0.8.2 64-bit 1 calculation Reviewed

Tests tools/process-test/test-link.mjs, tools/process-test/test-draw4.mjs

Channel counts and counts with spare

64-bit

How many analog and digital inputs and outputs the I/O list needs, and how many once the spare allowance is added.

with spare = ⌈n × (1 + spare)⌉

n × (1 + spare) is evaluated in 64-bit before Math.ceil. For spare 0.1 and n = 100 the product is 110.00000000000001, so the result is 111 where 110 is meant; n = 50 gives 56. The plain counts and the total are integer sums.

  • process/controls.js
  • lines 116 to 121
  • CadCraft 0.8.2
export function ioSummary(list, spare = 0.2) {
  const n = { AI: 0, AO: 0, DI: 0, DO: 0 };
  for (const r of list.rows) if (n[r[2]] !== undefined) n[r[2]]++;
  const withSpare = Object.fromEntries(Object.entries(n).map(([k, v]) => [k, Math.ceil(v * (1 + spare))]));
  return { counts: n, spare, withSpare, total: Object.values(n).reduce((a, b) => a + b, 0) };
}

In detail

Method
Count per type (AI, AO, DI, DO); with spare = ⌈count × (1 + spare)⌉; total = the sum of the four counts.
Number types
Integer counts; the spare factor is a 64-bit number (default 0.2).
Limits
With spare 0.1, 100 channels give 111 and 50 give 56, because 100 × (1 + 0.1) evaluates to 110.00000000000001. With the default 0.2 the stored factor is slightly below 1.2, so a whole-number product is never pushed above itself.

Approach: 64-bit, direct

Every value is a 64-bit number from input to result. Formulas are closed form or a direct solve, and every comparison uses a stated tolerance.

Every policy that takes this approach