code_junkie

Turn while loop into math equation?

Nov 13th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.37 KB | None | 0 0
  1. float a = someValue;
  2. int b = someOtherValue;
  3. int c = 0;
  4.  
  5. while (a <= -b / 2) {
  6. c--;
  7. a += b;
  8. }
  9. while (a >= b / 2) {
  10. c++;
  11. a -= b;
  12. }
  13.  
  14. c = floor((a+b/2)/b)
  15. a = a - c*b
  16.  
  17. x = (a+b/2)/b
  18. y = (a-b/2)/b
  19. c = (x≤0)*(ceiling(x) - 1 + (x is integer))
  20. +(y≥0)*(1 + floor(y))
  21.  
  22. c = (int)((a - (b / 2)) / b + 1);
  23. a -= c * b;
  24.  
  25. c = ((int) a + b / 2 * sign(a)) / b
Add Comment
Please, Sign In to add comment