You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've identified a potential issue in the round-robin distribution logic within the money allocation. It looks like the index variable is being reset to 0 at the start of each iteration in the while loop.
## round-robin allocation of any remaining pennies
ifresult.size > 0
whileremaining_amount != 0
index=0
amount_to_distribute=[1,remaining_amount.abs].min
ifremaining_amount > 0
result[index] += amount_to_distribute
remaining_amount -= amount_to_distribute
else
result[index] -= amount_to_distribute
remaining_amount += amount_to_distribute
end
index += 1
ifindex > result.size
index=0
end
end
end
As a result, the amount_to_distribute is always applied to the first element of the result array (result[0]), preventing the intended round-robin distribution across all elements.
To address this, I've made a change where the index variable is initialized outside the while loop. This adjustment allows the index to retain its value across iterations, ensuring that the amount_to_distribute is correctly applied to each element in the array in a round-robin manner.
I noticed this issue hasn't been caught in tests, likely because the remaining_amount usually doesn't exceed 1 after distribution, so it didn't produce erroneous results. However, I thought it would be a good idea to correct this to ensure the functionality works as intended for all possible cases.
@Mongey and @semmons99, since you have been working on this, it may be of particular interest to you.
I'm new here, and this would be my first contribution. I'm eager to hear any feedback or suggestions you might have!
Thank you!
The text was updated successfully, but these errors were encountered:
Hello everyone 👋,
I've identified a potential issue in the round-robin distribution logic within the money allocation. It looks like the index variable is being reset to 0 at the start of each iteration in the while loop.
money/lib/money/money/allocation.rb
Lines 43 to 63 in 092ecc7
As a result, the
amount_to_distribute
is always applied to the first element of the result array (result[0]
), preventing the intended round-robin distribution across all elements.To address this, I've made a change where the index variable is initialized outside the while loop. This adjustment allows the index to retain its value across iterations, ensuring that the
amount_to_distribute
is correctly applied to each element in the array in a round-robin manner.This is the corresponding pull request: #1073
I noticed this issue hasn't been caught in tests, likely because the
remaining_amount
usually doesn't exceed 1 after distribution, so it didn't produce erroneous results. However, I thought it would be a good idea to correct this to ensure the functionality works as intended for all possible cases.@Mongey and @semmons99, since you have been working on this, it may be of particular interest to you.
I'm new here, and this would be my first contribution. I'm eager to hear any feedback or suggestions you might have!
Thank you!
The text was updated successfully, but these errors were encountered: