Just keep in mind that the posterior should be continuously differentiable. You can write an inefficient linear function to truncate or a more efficient logarithmetic one with binary search, but however you do it, it's going to break differentiability w.r.t. the integer.
You can also allocate things that are larger and only use some of them if it's just a placeholder. But no way to get it the right size without hacking.