Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating a logarithmic percentage

I have an application monitoring a number of data points and I'm performing colouring based on the linear percentage fading between green, yellow and red. This however doesn't accurately visualise the problem as the higher the number the exponentially worse it is. The range is 0 -> 30000, how can I generate what I think would be termed a logarithmic percentage value rather than a linear one?

like image 427
Hawxby Avatar asked Jan 17 '26 21:01

Hawxby


2 Answers

I assume by "logarithmic percentage" you want to map your data to the range [0, 100] on a logarithmic basis. You can try something like this:

double Scale(int val)
{
    if (val <= 1)
        return 0; // log is undefined for 0, log(1) = 0
    return 100 * Math.Log(val) / Math.Log(30000);
}
like image 104
bmm6o Avatar answered Jan 19 '26 16:01

bmm6o


Use a function of the following form.

f(x) = s bx / 30000 + t

We know that 0 should map to 0% and 30000 should map to 100%.

f(0) = 0
f(30000) = 100

These imply the following system of equations.

s + t = 0
s b + t = 100

The solution (with respect to b) is the following.

s = 100 / (b - 1)
t = -100 / (b - 1)

Pick a particular b > 1 value (say b = 10). Then you get the following solution.

s = 100 / 9
t = -100 / 9

That is, the function f(x) is the following.

f(x) = (100 10x / 30000 - 100) / 9

You can see a plot of this function here: Wolfram Alpha

In C# this will look like the following.

double x = ...;

double b = 10.0;
double s = 100.0 / (b - 1);
double t = -100.0 / (b - 1);

double f = s * Math.Pow(b, x / 30000.0) + t;
like image 42
Timothy Shields Avatar answered Jan 19 '26 15:01

Timothy Shields



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!