Turn while loop into math equation?

后端 未结 4 1023
无人及你
无人及你 2021-02-05 10:56

I have two simple while loops in my program that I feel ought to be math equations, but I\'m struggling to convert them:

float a = someValue;
int b = someOtherVa         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 11:43

    I think you want something like this:

    c = ((int) a + b / 2 * sign(a)) / b
    

    That should match your loops except for certain cases where b is odd because the range from -b/2 to b/2 is smaller than b when b is odd.

提交回复
热议问题