Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse a percentage

I have a percentage, it ranges from 50% to 0%.

I need the values to be mirrored, so:

0% now equals 50%
1% = 49%
25% = 25%
48% = 2%
50% = 0%

etc.

Thanks for any help!

like image 286
Tom Gullen Avatar asked Jul 29 '10 11:07

Tom Gullen


2 Answers

You can use j = max_i - i + min_i where the two constants min_i and max_i are the lower and upper limit of the range.

If i is always between 0 and 50 then you can just write j = 50 - i.

like image 71
Mark Byers Avatar answered Nov 15 '22 08:11

Mark Byers


It looks like you want to define a function like this:

(x)      f(x)
 0        50
 1        49
 2        48
 :         :
48         2
49         1
50         0

Then the function is simply:

f(x) = 50 - x

More generally, if x is between low and high inclusive, then:

f(x) = (high + low) - x

Other functions of interest

Here are some other common functions:

(x)    f(x)___
 0       0    |
 1       0    3
 2       0 ___|
 3       1    |
 4       1    3     f(x) = x / 3
 5       1 ___|           where / is integer division
 6       2    |
 7       2    3
 :       : ___|

(x)    f(x)___
 0       0    |
 1       1    3
 2       2 ___|
 3       0    |
 4       1    3     f(x) = x % 3
 5       2 ___|           where % is integer remainder
 6       0    |
 7       1    3
 :       : ___|

Both of the above are sometimes combined when indexing a 2-dimensional table:

  ______4 columns______
 /                     \
 _______________________     (x)   row(x)   col(x)
|     |     |     |     |     0      0        0
|  0  |  1  |  2  |  3  |     1      0        1
|_____|_____|_____|_____|     2      0        2      row(x) = x / 4
|     |     |     |     |     3      0        3      col(x) = x % 4
|  4  |  5  |  6  |  7  |     4      1        0
|_____|_____|_____|_____|     5      1        1      x = row(x) * 4 + col(x)
|     |     |     |           6      1        2
|  8  |  9  | ... |           7      1        3
|_____|_____|_____|           :      :        :
like image 31
polygenelubricants Avatar answered Nov 15 '22 09:11

polygenelubricants