Solve Integer division in floating point context

Viewed 14236

When doing:

double a = 1000000000 / FPS;

It gives me the warning:

Integer division in floating point context.

How can I solve this?

5 Answers

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;

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.

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

to get rid of this warning use a helper variable you have

double a = 1000000000 / FPS;

it become

double b =1000000000;
double a = b / FPS;

double a = 1000000000.00 / FPS; It is true.

Related