开发者

How to convert a number range from 0-200 into 1-5 range

I have a value from 0 to 200 where 0 is better quality and 200 the wors开发者_JS百科t quality.

How could I convert (In obj-c/cocoa framework) that to an integer from 0-5 being 5 best?.

For example 200 would be 0 and 0 would be 5.


In general case if you have to transform Q = [A, B] to Q' = [A', B'], where f(A) = B' and f(B) = A', then an arbitrary X in space [A, B] will have for [A', B'] the following value:

X' = X * k + d;

where

k = (B' - A') / (A - B);

d = A' - B * k;

So, for your case we have A = 200, B = 0 and A' = 5, B' = 1, resulting:

k = -1/50

d = 5

an arbitrary value x from [0, 200] space will be translated as follow:

x' = x * (-1 / 50) + 5;


Hopefully the rounding works here:

int input;
int output = 5 - (int)floorf( ((float)input)/40.0f);

You may get the same results by just doing

int output = 5 - (input/40);

but it depends on your compiler's math settings.


Let x in 0..200. Do (200 - x) / 40 if you want a result between 0 and 5, or (200 - x) / 50 + 1 if you want something between 1 and 5.


I think should work where [0-200] is your quality score. 5 - ([0-200] / 40)


Alexander C.'s suggestion of (200 - x) / 50 + 1 is mathematically correct if the output should be interpreted as the sampled point within the [1,5] interval. With this formula, an output of 5 is only possible with an exact input of 0 (taken from [0,200]).

However, I expect that the original poster intends that each integer in [1,5] be representative of a subinterval of [0,200]. For example, that 5 corresponds with subinterval [0,39], and 4 with [40, 79] etc. If this is the intention, Alexander C.'s solution is a mathematically correct solution for a slightly different problem, in which case Stephen Furlani's approach is preferred.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜