Why this for loop is infinite?

eshkere111222333 :

I have such for loop and when step is (0;1) it becomes infinite. If step is [1;..) it works well.

  public interface FindMinI {
    double function(double x);

    static double findMinOfFuncOnInterval(int begin, int end, double step, FindMinI func)
    {
        double min = Double.MAX_VALUE;

        for (int i = begin; i <= end ; i += step) {

            if(func.function(i) <= min)
                min = func.function(i);

        }
        return min;
    }
 }
game0ver :

If you try with step between (0,1) this will be casted to int when adding to i, as a result you will add 0 to i in every iteration which will lead to infinite loop!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=476183&siteId=1