From 0a2559beba96b9857132f1844b2c3cf3378dbb23 Mon Sep 17 00:00:00 2001 From: Josh Buckner Date: Thu, 17 Sep 2026 10:21:46 -0400 Subject: [PATCH] Compute unit-count products in fmatch's feasibility check as doubles On a large problem the products of unit counts in the "recognizably infeasible" check overflow integer arithmetic: with 14,748 treated units and 372,150 controls, nt * mxc is about 5.5 billion, so it becomes NA and the enclosing if() stops with "missing value where TRUE/FALSE needed" rather than reporting that the restrictions cannot be met. Computing the two products in double precision leaves the comparisons unchanged and lets the check do its job. Co-Authored-By: Claude Opus 5 --- R/fmatch.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/R/fmatch.R b/R/fmatch.R index d348126c..97762734 100644 --- a/R/fmatch.R +++ b/R/fmatch.R @@ -137,9 +137,14 @@ fmatch <- function(distance, stop('Cannot choose "(_End_)" as unit name') ## Bypass solver if problem is recognizably infeasible + ## The products below are counts of units, which on large problems can + ## exceed .Machine$integer.max (e.g. 1.5e4 treated x 3.7e5 controls). + ## Integer overflow would make them NA and the `if` would then stop with + ## "missing value where TRUE/FALSE needed" instead of reporting the + ## infeasibility, so compute them in double precision. if ( (mxr >1 & nt/mxr > n.mc) | #max.row.units too low - (mxr==1L & nt * mnc > n.mc) | #min.col.units too high - (nt * mxc < n.mc)) { #max.col.units too low + (mxr==1L & as.numeric(nt) * mnc > n.mc) | #min.col.units too high + (as.numeric(nt) * mxc < n.mc)) { #max.col.units too low out <- as.data.frame(distance, row.names = NULL) out$solution <- rep(-1L, narcs)