返回未按预期返回值

发布于 01-17 04:02 字数 327 浏览 4 评论 0原文

我试图将变量时间(浮点数)四舍五入到百位。 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴2025-01-24 04:02:22

因此,如果您查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文