解决浮点上下文中的整数除法

Solve Integer division in floating point context

做的时候:

double a = 1000000000 / FPS;

它给了我警告:

浮点上下文中的整数除法。

我该如何解决这个问题?

你有两个选择。在末尾添加.0:

double a = 1000000000.0 / FPS;

或在末尾加d。

double a = 1000000000d / FPS;

出现警告是因为您将两个整数1000000000FPS相除,而您要将结果赋值给的变量是double,一个浮点类型。

您想要的是浮点除法
为此,除法的操作数之一必须是浮点类型。

您可以通过以下方式进行:

1000000000.0 / FPS; // make operand a floating point type (double)
1000000000.0f / FPS; //(float)
1000000000d / FPS; //(double)
(double)1000000000 / FPS; // cast operand to a floating point type (may be necessary if your operand is a variable)

当然你可以让FPS只是一个浮点变量而不是整数变量。

双 a = 1000000000.00 / FPS;是真的。

假设您正在使用这样的变量 -

int totalPresentStudentCount,count;
(totalPresentStudentCount/count) * 100.00

然后简单地添加 1.0 与任何这样的双打 -

int totalPresentStudentCount,count;
(totalPresentStudentCount*1.0/count) * 100.00