There is a math trick which is used by abusing the power of two.
If you have bonus attack abilities that give 1,2,4,8,16. and store them into an array
Let's say you want to add 7 attack.
loop
if bonusAttack[int] less than or equal to valueYouWant and total is lower than or equal value you want
add bonusAttackAbility[int] to your unit
set total = total + bonusAttack[int]
endloop
in which case it will add 4 + 2 + 1