I have a “thing” (details not important) that outputs a number between 0 and + infinity. The number is measure of certain input factors where a high number is bad and a low number good.
I need to add to this a sensitivity setting – so the caller can specify a number that essentially scales the output.
Ideally I would like that scale to be between -1 and 1. 0 represents normal. As the scale increases towards 1 the sensitivity increases and the output number decreases. As the scale decreases towards -1 the sensitivity decreases and the output number increases.
So far, I have this:
static decimal GC(decimal inVal, decimal gc)
{
decimal nOUT = inVal;
if( gc > 0 )
{
nOUT = Math.Max( inVal * (1 - gc), 0 );
}
else if( gc < 0 )
{
if( gc == 1 ) gc = 0.9M;
decimal d = Math.Abs(gc);
nOUT = inVal * ( (1 / d) );
}
return nOUT;
}
GC being my scale function and gc being the input factor.
This works OK for towards 1, with output being
IN = 3.0, OUT = 3.0, gc = 0
IN = 3.0, OUT = 2.7, gc = 0.1
IN = 3.0, OUT = 2.4, gc = 0.2
IN = 3.0, OUT = 2.1, gc = 0.3
IN = 3.0, OUT = 1.8, gc = 0.4
IN = 3.0, OUT = 1.5, gc = 0.5
IN = 3.0, OUT = 1.2, gc = 0.6
IN = 3.0, OUT = 0.9, gc = 0.7
IN = 3.0, OUT = 0.6, gc = 0.8
IN = 3.0, OUT = 0.3, gc = 0.9
IN = 3.0, OUT = 0.0, gc = 1.0
But not for towards -1 without output being
IN = 3.0, OUT = 3.0, gca = 0
IN = 3.0, OUT = 30.0, gca = -0.1
IN = 3.0, OUT = 15.0, gca = -0.2
IN = 3.0, OUT = 10.0, gca = -0.3
IN = 3.0, OUT = 7.5, gca = -0.4
IN = 3.0, OUT = 6.0, gca = -0.5
IN = 3.0, OUT = 5.0, gca = -0.6
IN = 3.0, OUT = 4.3, gca = -0.7
IN = 3.0, OUT = 3.8, gca = -0.8
IN = 3.0, OUT = 3.3, gca = -0.9
IN = 3.0, OUT = 3.0, gca = -1.0
I need the output number to get bigger as gca approaches -1.
The second problem I have with this is one of scale. At most, the number will be 10 times bigger. I think I need to introduce another number, say scale, where gca represents the proportion of scale that is applied +/- to the output number but I dont know how.
Can any one help? Either fixing this or with a better way!
Thanks
I’m not quite sure I understand your problem correctly, but perhaps this will work?
This expression becomes singular as
gcapproaches -1, andnOUTdiverges toward infinity hyperbolically. This is the natural “opposite” of the0 < gc < 1case.If you want to limit the number to being 10 times bigger, you could just rescale
gcby multiplying by 0.9; this will limit the(1 + gc)term to 0.1, which corresponds to a multiplication by 10. For example:Also note that your use of
Math.Maxin the0 < gc < 1case is redundant.To give you an idea of how this scaling behaves, here’s a plot I made of
nOUTagainstgcforinVal = 5, with a limiting value of 10:You should note, however, that imposing this limit on how much larger the number can be makes the scaling asymmetric and less “nice”. For smaller limiting factors than 10, you’ll see a cusp at
gc = 0; for example, with a limiting factor of 1.5:Another approach would be to simply cap the scaling factor at 0.9, rather than rescaling the factor itself as I suggested above. This eliminates the cusp at
gc = 0and instead just flattens it off when it reaches that limiting factor (10 in this case). You could achieve this like so:And here’s a graph to compare the two methods (blue line is first method; red line is second):