开发者

Relative values from max values Maths Problem

This is more Maths related than a CS question, but I开发者_如何学C would like the answer in CS.

What I want to do is find a value relative to the max value. For instance say the value is 3. The max is 6. Therefore this value is 0.5 of the max value.

It can be implemented quite easily like this: currentValue / max

However, say if now I have negatives. Say the max is now -6 and the current value is -7. I would need to use the minimum value for this. So say the minimum value is -8. In this case, -7 will be 0.5 of -6.

However, how can I express finding the current value as an equation for negatives (using max and min)?


I'm going to assume that when max is positive, min is zero, since that's what your example suggests.

The final value you seek, given an input is the proportion of the change from min to max that the change from min to input consists of. It's actually easier in a formula:

Final value = (input - min) / (max - min)

For example:

min = 0
max = 6
input = 3
Final value = (3 - 0) / (6 - 0) = 3 / 6 = 0.5

min = -8
max = -6
input = -7
Final value = (-7 - -8) / (-6 - -8) = 1 / 2 = 0.5


In effect, you want to treat the minimum value as if it were the zero on your axes. To do that, just subtract min from all values, giving

(currentValue - min) / (max - min)

You should check to make sure you don't divide by zero.


case 1: if zero comes in between the max number and current value,you can use the distance between current value and zero over total distance between current value and max value .

case 2: if max value comes in between current value and zero , you can use the distance between current value and max over the total distance between zero and current value.

case 3:if current value comes in between max and zero use distance between zero and current value over distance between zero and max value.

since you need a value from o through 1,in your choice of numerator you can find and use the smaller segment.for ex- if the currentvalue and zero segment is shorter than the segment between zero and max value,use the former as numerator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜