Simple division operation returning zero?

前端 未结 5 1449
醉酒成梦
醉酒成梦 2021-01-29 13:48

What am I missing?

float stepSize = 0.0f;
int activeCircleRadius = 10;
int numSteps = 24;

AiLog.v(\"init activeCircleRadius \" + activeCircleRadius + \" numStep         


        
5条回答
  •  孤独总比滥情好
    2021-01-29 14:29

    Here both variables are integers so you are performing integer division:

    activeCircleRadius / numSteps
    

    The result of integer division is an integer. The result is truncated.

    To fix the problem, change the type of one (or both) variables to a float:

    float stepSize = 0.0f;
    float activeCircleRadius = 10;
    

    Or add a cast to float in the division expression:

    stepSize = (float)activeCircleRadius / numSteps;
    

提交回复
热议问题