简单的除法运算返回零? [英] Simple division operation returning zero?

查看:97
本文介绍了简单的除法运算返回零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我缺少什么?

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

AiLog.v("init activeCircleRadius " + activeCircleRadius + " numSteps "
                + numSteps);
stepSize = activeCircleRadius / numSteps;
AiLog.v("stepSize is " + stepSize);

操作后日志中的stepSize始终为零。是否将浮点数除以int?

stepSize is always zero in the log after the operation. Does it have to do with dividing a float by an int?

推荐答案

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

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;

这篇关于简单的除法运算返回零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆