How to convert a range of numbers from 0-200 to 1-5

I have a value from 0 to 200, where 0 is the best quality and 200 of the worst quality.

How can I convert (in obj-c / cocoa framework) so that an integer from 0 to 5 is the best?

For example, 200 will be 0 and 0 will be 5.

+4
source share
5 answers

Hope rounding works here:

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

You can get the same results just by doing

 int output = 5 - (input/40); 

but it depends on your compiler math settings.

+3
source

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

 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 , as a result we get:

 k = -1/50 d = 5 

an arbitrary value of X from [0, 200] will be translated as follows:

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

Let x be in 0..200. Make (200 - x) / 40 if you want to get a result from 0 to 5 or (200 - x) / 50 + 1 if you need something between 1 and 5.

+1
source

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

+1
source

The proposal of Alexander C. (200 - x) / 50 + 1 is mathematically correct if the output should be interpreted as a selected point in the interval [1,5]. Using this formula, output 5 is possible only with the exact input of 0 (taken from [0,200]).

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

0
source

Source: https://habr.com/ru/post/1336999/


All Articles