veltaandrg / JavaExercises

0 stars 0 forks source link

Float loops #3

Open RodionGork opened 9 years ago

RodionGork commented 9 years ago

Let us try the same loop as in #2 but with non-integer variables:

    float i = 0;
    while (true) {
        if (!(i < i + 1f)) {
            break;
        }
        i += 1;
    }
    System.out.println(i);

What is the result? What is the result of i + 1 in this case, and what it was if i was int, as in previous exercise?

Now change the variable type to double and modify the code so it multiplies by 1.1 instead of increment:

    double i = 1;
    while (true) {
        if (!(i < i * 1.1)) {
            break;
        }
        i *= 1.1;
    }
    System.out.println(i);

What is going to be printed? How many iterations it will take to come to an end? And if you know the count of operation, then to which power of 1.1 the result is technically "equal"? Can you calculate which power of 10 it is (roughly)?