2

I have this problem where I want to keep a list of values and normalize them so that the sum of all values is 1.

I found this question, but there is one difference. I would like to have 1 value that is fixed. Because, there are 4 GUI sliders and I drag one. The other 3 should update accordingly so that the total sum is 1.

// Let's keep 0.6 fixed and try to normalize the rest
List<float> categories = new List<float>() { 0.6f,0.3f,0.25f,0.25f };

// find the max value of the rest of the values that is not the fixed number
float maxval = categories.Where(s => s != 0.6f).Max();

if (maxval != 0) // check if not all other 
{
    // calculate the ratio
    float ratio = (1 - 0.6f) / maxval;

    foreach (float s in categories)
    {
        if (s != 0.6f)
        {
            s *= ratio;
        }
    }
}
else // all values are 0, so just add the rest value
{
    // do something else that is correct
}

Unfortunately this example will return:

{ 0.6f, 0.4f, 0.33f, 0.33f } => sum is 1.66f

And there is something that I'm misunderstanding. Can someone give me a clue on how to get the rest of the values normalized to the rest sum (0.4f) so that the total sum of all values is 1?

3
  • Don't replace your values during the drag operation, use a second set of values for display. That way you can do all your calculations on the pre-drag values. If you do calculations based on values computed during the drag, your rounding error will accumulate very rapidly.
    – Ben Voigt
    Sep 6, 2013 at 16:12
  • When you got it working, can you please mark the question as answered? If its not working as expected, can you let us know whats going wrong?
    – SinisterMJ
    Sep 6, 2013 at 20:12
  • @AntonRoth I'll be at the office on monday ;)
    – Marnix
    Sep 6, 2013 at 20:23

2 Answers 2

6

You are calculating it wrongly.

If you have a 4 elements, and 1 of those is fixed, and the total sum needs to be 1, you would go like:

ratio = (1-fixedElement) / sumOfAllOtherElements;

EachNonFixedElement *= ratio;

You are calculating the ratio just with the max value, but you need to calculate it with the sum of all non-fixed elements.

0

Among other problems, this loop doesn't do what you expect:

foreach (float s in categories) {
    if (s != 0.6f) {
        s *= ratio;
    }
}

s is a local copy of one of the values from categories. You modify the copy, and then it goes out of scope and your new value disappears. You never modified categories.

C# foreach cannot be used to modify a collection.

Furthermore, to avoid accumulating large rounding error, you should store to a new collection and not replace the values in categories, at least until the drag operation ends.

1
  • Thanks I know that. I am actually looping over my own class GuiComponent, but this was just for the sake of simplicity.
    – Marnix
    Sep 6, 2013 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.