且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

简单的除法运算返回零?

更新时间:2023-02-19 23:24:08

这里两个变量都是整数,因此您执行整数除法:

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.

要解决此问题,请将一个(或两个)变量的类型更改为 float

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

float stepSize = 0.0f;
float activeCircleRadius = 10;

或者在分部表达式中添加一个转换为float:

Or add a cast to float in the division expression:

stepSize = (float)activeCircleRadius / numSteps;