a = 1541
b = 1575
c = 1512
# I want to ratio the sum of these numbers to 128
total = a b c
rounded_a = round(a*128/total) # equals 43
rounded_b = round(b*128/total) # equals 44
rounded_c = round(c*128/total) # equals 42
total_of_rounded = rounded_a rounded_b rounded_c # equals 129 NOT 128
# I tried the floor
floor_a = math.floor(a*128/total) # equals 42
floor_b = math.floor(b*128/total) # equals 43
floor_c = math.floor(c*128/total) # equals 41
total_of_floor = floor_a floor_b floor_c # equals 126 NOT 128
# The exact values
# a: 42.62057044
# b: 43.56093345
# c: 41,81849611
The question is, how can I reach the total 128?
Note: I should stay at integer, not floating numbers. Note 2: I can write a correction function which like adding 1 to total but it doesn't seem right to me.
CodePudding user response:
A possibility: round a
and b
down, then add the missing parts to c
.
a = 1541
b = 1575
c = 1512
total = a b c # 4628
ra = a * 128 // total
rb = b * 128 // total
rc = (c * 128 (a * 128)%total (b*128)%total) // total
print(ra,rb,rc)
# (42, 43, 43)
print(ra rb rc)
# 128
CodePudding user response:
This is the way for it:
a = 1541
b = 1575
c = 1512
# I want to ratio the sum of these numbers to 128
total = a b c
total_of_round = round((a*128/total) (b*128/total) (c*128/total))
print (total_of_round)
Ouput:
128