返回未按预期返回值
我试图将变量时间(浮点数)四舍五入到百位。 (0.01,0.02,...)
float Time = 0.0;
float Time += 1/frameRate;
return( (round(Time*100)) /100);
这就是我尝试的,但我的结果四舍五入到最接近的整数(0.0,1.0,...)
然后我尝试了这个:
float x = round(Time*100);
return(x/100);
并且它起作用了。我解决了自己的问题,但我正在努力找出为什么它有效而另一个却不起作用。
I am trying to get the variable time (a float) rounded to the hundreth's place. (0.01, 0.02, ...)
float Time = 0.0;
float Time += 1/frameRate;
return( (round(Time*100)) /100);
This is what I attempted, but my results were rounded to the nearest integer (0.0, 1.0, ...)
Then I tried this instead:
float x = round(Time*100);
return(x/100);
and it worked. I solved my own issue, but I'm struggling to find out why it works while the other doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

因此,如果您查看 round 文档,您可以看到
round(x)< /code> 返回一个 int。
因此,当您编写
round(x)/100
时,您将 int 除以 100,从而导致意外行为。但在 float x = round(y) 中,您显式地将其类型转换为 float,因此除法按预期工作。
So if you look at round documentation, you can see that
round(x)
returns you an int.So when you write
round(x)/100
, you are dividing an int by 100, causing unexpected behaviour.But in
float x = round(y)
, you are explicitly typecasting it into float, so the division works as expected.