Search code examples
c++mathpercentageblendingsmoothing

Step Independent Smoothing


I often smooth values by blending percentages and inverse percentages with the below:

current_value = (current_value * 0.95f) + (goal_value * 0.05f)

I'm running into a situation where I would like to perform this action n times, and n is a floating point value.

What would be the proper way of performing the above, say 12.5 times for example?


Solution

  • I appreciate the help! I have been trying several things related to compound interest, and I believe I may have solved this with the following. My ultimate goal in mind (which was actually unstated here) was to actually do this with very little iterative processing. powf() may be the most time consuming piece here.

    float blend_n(float c, float g, float f, float n)
    {
        if (g != 0.0f)
            return c + ((g - c) / g) * (g - g * powf(1.0f - f, n));
        else
            return c * powf(1.0 - f, n);
    }
    

    It's late here, and my redbull is wearing off so there may be some parts that could be factored out.

    Usage would be setting c to the return value of blend_n ...

    Thanks again!

    [EDIT] I should explain here that c is the (current) value, g is the (goal) value, f is the (factor), and n is the (number of steps) [/EDIT]

    [EDIT2] An exception has to be made for goal values of 0, as it will result in a NaN (Not a Number) ... Change done to the code above [/EDIT2]