# Thread: Help with a condition function

1. ## Help with a condition function

Hello everyone,

I need a little help.

I need a function that will do the following:

I have two variables ( can have more if needed)

If scale is 0-5 then ticks = 5
If scale is 6-10 then ticks = 2.5
If Scale is 11-15 then ticks is 1.25
...AND SO ON.

I would code with conditionals, but It needs to be good forr an unlimited number of scales.

I need to set ticcks based on scale

Thanks 2. I would code with conditionals, but It needs to be good forr an unlimited number of scales.
You need to find out how to map the scale to the ticks.
As a hint, notice that:
scale = [0-5] => ticks = 5 / 1
scale = [6-10] => ticks = 5 / 2
scale = [11-15] => ticks = 5 / 4 3. So it looks like I could do something like:
Code:
```i = 0;

for(j = 6;j<=scale;j=j+5)
{
i++;
}

ticks = 5/(2^i);```

Does this seem like it would work? 4. Does this seem like it would work?
Gee, willickers. Your computer won't explode if you try it. 5. You could use the forumla:

ticks = 5.0 / (1 << ((scale-1)/5));

Of course, you'd have to treat the zero case differently, unless you shift your ranges to the more convenient 0-4, 5-9, 10-14, 15-19, ... and get rid of the "-1" above. 6. Code:
`(2^i)`
This may not do what you're expecting. ^ is the operator for bitwise XOR and not for raising a number to a power. You use the math.h function pow() for that.
But since you're using 2 as a base and integers as exponent you can use left bitwise shifting instead which is very fast but will not give the value expected if shifted out of bonds.
"2^i" in math can be written as "1 << i" in C/C++ Popular pages Recent additions 