Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math Question in C or Objective C

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

like image 591
Xcoder Avatar asked Jun 17 '09 03:06

Xcoder


1 Answers

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.

like image 86
JSchlather Avatar answered Nov 11 '22 14:11

JSchlather