Fix float32 truncation in balanced_allocation iteration loop (#1203)#1205
Merged
brendancol merged 1 commit intomasterfrom Apr 15, 2026
Merged
Fix float32 truncation in balanced_allocation iteration loop (#1203)#1205brendancol merged 1 commit intomasterfrom
brendancol merged 1 commit intomasterfrom
Conversation
_allocate_biased and _allocate_from_costs cast the allocation array to float32 internally. The balancing loop then compared these float32 values against float64 source IDs. For non-integer IDs (e.g. 0.1, 0.3) the float64->float32->float64 round-trip produces a different value, so alloc == sid never matched, weights were all zero, and the loop exited on the first iteration without balancing. Fix: keep the allocation array as float64 through the iteration loop. The final cast to float32 happens at the return statement.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1203.
Summary
_allocate_biasedand_allocate_from_costscast the allocation array tofloat32internally. The balancing loop then compared thesefloat32valuesagainst
float64source IDs. For non-integer source IDs (e.g. 0.1, 0.3),float64 -> float32 -> float64produces a different value, soalloc == sidnever matched and the loop exited on the first iteration without balancing.
float64for the allocation array. The finalcast to
float32already happens at the return statement.the fix.
Test plan
test_non_integer_source_ids-- three sources with IDs 0.1, 0.3, 0.7on a 12x12 grid. Verifies each territory gets at least 20% of cells.
test_balanced_allocation.pytests still pass.