我的浮动操作有问题 [英] something wrong with my float operation

查看:155
本文介绍了我的浮动操作有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须缺少某些东西,这有什么问题吗?

i must be missing something, whats wrong with this?

float controlFrameRate = 1/60;

它应该是分配一些像0.0166666667,但它出来0.00000等是视觉工作室只是躺在我?

It should be assigning something like 0.0166666667 but its coming out 0.00000 etc. is visual studio just lying to me?

推荐答案

这是因为 1/60 是0,因为整数除法截断。这是用来初始化浮动,给出 0。你可以通过使RHS表达式浮动在第一位来修复它:

That is because 1/60 is an integer, which is 0 because integer division truncates. This is used to initialize the float, giving 0. You can fix it by making the RHS expression a float in the first place:

float controlFrameRate = 1.0f/60;

float controlFrameRate = 1/60.0f;

在C ++中, 1 42 等是 int 1.0 3.1416 double f 1.0f 使文字a float 。请注意,在上面的示例中可以省略 f 。然而,如果double的值超出浮点范围,则将double赋给浮点可能会有问题。

In C++, literals such as 1, 42 etc. are int, 1.0, 3.1416 are double, and the f in 1.0f makes the literal a float. Note that the f could have been omitted in the examples above. However, assigning a double to a float could be problematic if the double's value goes beyond the range of a float.

这篇关于我的浮动操作有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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