• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Maths problem

I have a number and I want to break it down into integers by %

but I want to guarantee that the sum of the breakdown equals the original number. i.e. if there is a rounding error, I want it corrected.

eg

number 998

10% = 100 (rounded up)
30% = 300 (rounded up)
30% = 300 (rounded up)
30% = 300 (rounded up)

for a total of 1000

I can do it the hard way. but is there a simple algorithm ?




:rolleyes:

In most programming languages there is modulus operator that yiou can use to do you're rounding operations.

Let me show you illustartion.

Number_to_round <modulus operator> reminder_value
for very simple example

if (100 % 2 == 0)
// the number is less than 50 so round down to 0
else
// the number is greater than 50 so round up t o 100

I think from this simple example you should be able to accomodate this in your project and use this with some degree of success.

Let me know if I can be of any further assistance.

HTH,

Joshi.
 
veeeeeeeeeery quick answer cos hungry and glanced at thread
add all the rounded numbers up
=999

now you've got to loose 1 somewhere so choose the number with the fractional part closest to .5 and > 0.5 and zap it's fractional part

no repeat the adds of the rounded numbers and u get 998.

P.S. There might (probably) is a fairer way to "allocate" the zapping of the extra 1 but what I just scribbled might be a good way
 
veeeeeeeeeery quick answer cos hungry and glanced at thread
add all the rounded numbers up
=999

now you've got to loose 1 somewhere so choose the number with the fractional part closest to .5 and > 0.5 and zap it's fractional part

no repeat the adds of the rounded numbers and u get 998.

P.S. There might (probably) is a fairer way to "allocate" the zapping of the extra 1 but what I just scribbled might be a good way

aye, thats the logic all right. but I want the algorithm




:rolleyes:
 
ok part way through shepherds' pie so can spare a few more ticks but I'm not going to check this mmmmmmmmkay

array_percentages (0.2,0.1,0.7)
array_numbers ()
target = 998
tobezapped
bestsofar = 0

for n = 1 to max_elements(array_percentages)
array_numbers(n) = array_percentages(n) * target
next n

while sum(integer_part(array_numbers())) <> target
if sum(integer_part(array_numbers())) > target
for for n = 1 to max_elements(array_numbers)
if fractional_part (array_numbers(n)) - 0.5 >= bestsofar then
bestsofar = fractional_part (array_numbers(n)) - 0.5
tobezapped = n
end if
next n
array_numbers(tobezapped)=integer_part(array_numbers(tobezapped)) 'or you could subtract bestsofar+1
else
'do the sort of opposite to round up the number closest to fractional 0.5
end if
wend

now your answer is integer_part of all the elements in array_numbers
 
Last edited: 15:22
veeeeeeeeeery quick answer cos hungry and glanced at thread
add all the rounded numbers up
=999

now you've got to loose 1 somewhere so choose the number with the fractional part closest to .5 and > 0.5 and zap it's fractional part

no repeat the adds of the rounded numbers and u get 998.

P.S. There might (probably) is a fairer way to "allocate" the zapping of the extra 1 but what I just scribbled might be a good way

Whichever rounding scheme you choose you will introduce a rounding error. Either the totals will not add up in some cases or you have to accept different numerical values for identical percentages in other cases. The best way to distribute the error is probably to "dither" the untruncated numerical values before rounding although I suspect this won't actually achieve what you want.
 
the number of variables is an unknown
the % will be 2 dp
n will be an integer


:rolleyes:

Unknown? I thought you started with a given list of percentages (integers or otherwise) which in effect you had to scale up and round such their sum is some given integer and they deviate minimally as percentages of this sum from the originals.

That's the problem whose solution I sketched. But if you don't even know what the percentages are to start with it's an ill-posed problem.

(although if you know all the distinct percentages but not the number of repetitions of each, then it's a variant of the Frobenius Coin Problem)
 

Advertisers

Back
Top