14

When doing:

double a = 1000000000 / FPS;

It gives me the warning:

Integer division in floating point context.

How can I solve this?

L0raxeo C
  • 323
  • 2
  • 16
Dumm Coding
  • 151
  • 1
  • 1
  • 7

5 Answers5

21

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.

Raildex
  • 3,406
  • 1
  • 18
  • 42
  • the suffix `d` makes it a double already. adding a trailing zero doesnt make a difference – Raildex Jan 12 '21 at 08:30
  • It doesn't say that in your answer. Casting it to `float` won't work, as the value is out of range. – user207421 Jan 12 '21 at 08:34
  • I don't know what compiler you use, but casting to `float` works, even if the value is out of range https://javap.yawk.at/#tpUGn7 – Raildex Jan 12 '21 at 08:40
12

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;
L0raxeo C
  • 323
  • 2
  • 16
  • 8
    Maybe add why this works so the OP understands. Otherwise Good answer – The Grand J Jan 12 '21 at 05:10
  • 1
    It says he is a new contributor and this seems like a relatively simple idea. I believe he is a beginner. I know what it's like to be a beginner and to be bombed with complicated ideas. I want to make it simple for him :). if I thought the circumstance were to be different then I would definitely do that! – L0raxeo C Jan 12 '21 at 05:13
  • Not an F; add a D. F forces float. floats are almost entirely useless. You want doubles. – rzwitserloot Jan 12 '21 at 06:05
  • 1
    There is a third option, probaby more depending on what `FPS` is. You must explain why this works. – user207421 Jan 12 '21 at 06:39
2

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;
devio
  • 607
  • 9
  • 29
1

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
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
-3

double a = 1000000000.00 / FPS; It is true.

Allen
  • 88
  • 6