views:

2735

answers:

1

This is probably a silly and easy question, but it seems sometimes the simplest things give me more problems!

This formula is suppose to give me a number between 0 and 100.

(200 / 23) * Abs(Mod(2987, 23) - 23 / 2)

In objective C I coded it like this:

(200 / 23) * abs(2987 % 23) - (23 / 2);

Is the formula flawed (and doesn't give an answer between 0 and 100) or is my code wrong? It seems that my modulus isn't giving me the right result. Shouldn't it give me a one number integer?

Thanks

+13  A: 

Your code is wrong in objective C...

(200 / 23) * abs(2987 % 23) - (23 / 2);

Should be

(200 / 23) * abs((2987 % 23) - (23 / 2));

Which is just 73.9.

But this formula is also incorrect, you want values between 0 and 100. Your current formula does not reach 0, because a%23 has a range of 0-22, so the lowest value you can get if you subtract 23/2 and then take the absolute value is .5 (11-11.5 and 12-11.5). 22 would be the ideal number in this instance.

Also by subtracting 23/2 you get an uneven distribution, if you just multiplied the mod by 100/22, you would be better off. It would help to know what you are attempting.

Jacob Schlather
Indeed, he could have just written 73.9130434783 :-P.
Evan Teran
Perfect! I knew it was something simple...thanks.
Xcoder