Search code examples
javaintellij-ideafloating-pointsuppress-warningsinteger-division

Solve "integer division in floating-point context" warning


How does one solve the "integer division in floating-point context" warning, in this line of code:

int fps = 60;
double timePerTick = 1000000000 / fps;

Solution

  • When dividing two ints, you'll be using integer division, and only then promoting the result to a double, losing all the precision after the decimal point. You can use floating point precision by using a double literal instead:

    double timePerTick = 1000000000.0 / fps;
    // Here -----------------------^