Formula that gives linear increase in output when input doubles.

912 Views Asked by At

I am looking for a formula that will take x and output y like so:

1        100
2        400
4        700
8        1000
16       1300
...

Essentially, for every doubling of the input, output increases by a fixed value. In this case 300, offset by 100 but that's not important. What's important is that for every doubling of the input value, i want a linear increase in output.

But there's another problem. I cannot make use of power, square roots or logarithms etc. And i can't use decimal numbers. I.E 1.512457256.... I can however use integers up to 2^31 so i can represent 1.51 as 151 / 100 and work with that in my problem. The operations i have available to me are the simple ones as well as bitwise operations:

+, -, /, * and % &, |, ^, << and >>

Or in other words, addition, subtraction, division, multiplication and modulus... As well as bitwise AND, OR, XOR, Left Shift and Right Shift.

I can also use these simple functions: max(), min() and abs()

Can this be accomplished and if so, how?

Please try and keep it simple. I have practically no skills in maths. I don't work well with "advanced" notation.

EDIT:

Note that decimal accuracy is not important. Hence, as Jaume Oliver Lafont points out below, i can check which bit is set to one in the input value and work with that as the log2(x) if need be.

But at that point, which i neglected to mention, i can use ranges and hardcode the log values as well. But i would rather avoid it if possible. However, without log2(x) i now do realize how impossible this would be. Unless there's some very intelligent approximation of log2(x) one can use.

1

There are 1 best solutions below

5
On BEST ANSWER

The function is

$$f(x)=100+300\log_2(x)$$

At the points you give, $\log_2(x)$ is the position of the non-zero bit.

Can you write a log2(x) function?

Here are some ways to build such a function.

https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers