java - Why does Math.sqrt return 0 instead of the length of a vector? -


public static double[] creevec(double x, double y, double z){      //cree et retourne un tableau     double[] tab = new double[3];     tab[0]=x;     tab[1]=y;     tab[2]=z;     return tab; }  public static double longueur(double[] tab){      //calcule la longueur     tab = new double[3];     double carre = ((tab[0]*tab[0])+(tab[1]*tab[1])+(tab[2]*tab[2]));     return math.sqrt(carre); } 

and when called them result of math.sqrt() 0

    double[] v1 = vec3dtab.creevec(10, 20, 30);     double[] v2 = vec3dtab.creevec(4, 5, 6);      //affiche la longueur de v1     double longueurv1 = vec3dtab.longueur(v1);     system.out.println(longueurv1); 

the code tab = new double[3]; in longueur(double[] tab) method creates new double array object , initializes default value of 0.

hence returns 0.

please remove line , proper value.


Comments