Java float type in for loop

Question

What is the output of the following code?

public class Main {
  public static void main(String[] args) {
    // Initialize sum
    float sum = 0;

    // Add 0.01, 0.02, ..., 0.99, 1 to sum
    for (float i = 0.01f; i <= 1.0f; i = i + 0.01f)
      sum += i;/*from  w ww  .j  a  va 2 s  .  c  om*/

    // Display result
    System.out.println("The sum is " + sum);
  }
}


The sum is 50.499985



PreviousNext

Related