'Solve Integer division in floating point context

When doing:

double a = 1000000000 / FPS;

It gives me the warning:

Integer division in floating point context.

How can I solve this?



Solution 1:[1]

You have two options. Add .0 at the end as such:

double a = 1000000000.0 / FPS;

or adding d at the end.

double a = 1000000000d / FPS;

Solution 2:[2]

The warning occurs because you divide two integers 1000000000 and FPS, and the variable you want to assign the result to is double, a floating point type.

What you want instead is having a floating point division.
For this, one of the operands of the division has to be a floating point type.

You can do it in the following ways:

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)

and of course you can make FPS just a floating point variable instead of a integer variable.

Solution 3:[3]

Suppose if you are using a variable like this -

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

Then simply add 1.0 with any of the doubles like that -

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

Solution 4:[4]

double a = 1000000000.00 / FPS; It is true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Raildex
Solution 3 Gk Mohammad Emon
Solution 4 Allen