Ok this seems to work. Might have put more parenthesis in than needed.
$new_val = 1 + (($val - $min) * (99 / ($max - $min)))
문제
I'd like to do a tiny scoring, but I stumble at maths. I guess you will handle it :-) I have a few values: e.g. 124, 426, 100, 24, 41. The highest shell be MaxScore = 100 (here 426), and the minimum MinScore = 1 (here 24). Every value (here 124,100,41) between MaxScore and MinScore shell get an own Score, related to min and max.
With php max(), I get
$max = max(124, 426, 100, 24, 41);
426 as the highest and with min()
$min = min(124, 426, 100, 24, 41);
24 as the lowest value. Fine.
Next step could be like: 426 = 100 and 24 = 1. But the rest? What's the calculation? As you can see, it's not a programmer problem (not yet), rather a maths-issue. I hope I did make myself clear. Thank you!
해결책
Ok this seems to work. Might have put more parenthesis in than needed.
$new_val = 1 + (($val - $min) * (99 / ($max - $min)))
다른 팁
One thing you could do is apply each value to be $val * ($max - $min) / 100;
EDIT I had that backwards. It'll be $val / (($max - $min) / 100);
This takes the ratio of the difference between $min
and $max
divided into 100 pieces. So each piece will then have the ratio applied to it. This would make 100 be 100 / ((426 - 24) / 100) = 24.875
and it would reduce all values to a number between 1 and 100.
EDIT. To get the min to be 1 and the max to be 100 and maintain relationships between scores we need to apply the ratio and subtract a contstant.
Verified Andrew Nee's comment seems to do the trick.
php -r "$a = array(123,426,100,24,41);print_r(array_map(function($val){$min=24;$max=426;return 1 + (($val - $min) * (99/($max - $min)));},$a));"
Array
(
[0] => 25.380597014925
[1] => 100
[2] => 19.716417910448
[3] => 1
[4] => 5.1865671641791
)