fn shotmargins_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn shotmargins_counts(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value <= 9007199254740991.0 && value % 1.0 == 0.0) { return false; } i += 1; } true } fn shotmargins_compute(counts: vector) -> vector { if len(counts) != 4 { return []; } let total = sum(counts); if total <= 0.0 || total > 9007199254740991.0 { return []; } [(at(counts, 2) + at(counts, 3)) / total, (at(counts, 1) + at(counts, 3)) / total, total] } // Extract both individual bit frequencies from joint two-bit shots. // Bin order is 00,01,10,11. Returns [P(left=1), P(right=1), shots]. At least one shot is required. pub fn shotmargins_bits(counts: vector) -> vector { if !shotmargins_counts(counts) { return []; } let result = shotmargins_compute(counts); if !shotmargins_finite(result) { return []; } result }