Division always resulting in zero

1

Alpha result is only returning 0. Why?

package javaapplication4;

public class MediaMovelSuavizaçãoExp {


    public double CalculoPrevisao(double[] valores){

        double[] values = new double[valores.length];

        //Calculando o valor de alfa

        double alfa = 2 / ( values.length + 1);

        return alfa;
    }

}
    
asked by anonymous 04.11.2017 / 18:15

1 answer

5

You are doing integer splitting, so the result is integer, even though you later save it to double . Then divide a double , like this:

return 2.0 / (values.length + 1);
    
04.11.2017 / 18:21