0

I am trying to write a function to check a T score value and populate half of a 5x5 array.

public void calcTScores()
    {
        double temp = 0;
        double tempSp = 0;
        int n = 24;
        this.tScores = new String [5][5];
        for (int i = 0; i< this.Headers.length; i++){
             for (int j = 0; j<this.Headers.length; j++)
             {
                 if(i < j)
                 {
                     tempSp += (n-1)*this.SD[i] * this.SD[i] + (n-1)*this.SD[j] * this.SD[j];
                     tempSp = tempSp/(n+n-2);
                     tempSp = Math.sqrt(tempSp);
                     temp = tempSp * Math.sqrt(0.0833);
                     System.out.println(Math.sqrt(1/12));
                     temp = ((this.Mean[i] - this.Mean[j])/temp);
                     if(temp > 2.25 || temp< -2.25)
                     {
                         this.tScores[i][j] = "Y";
                     }
                     else 
                     {
                         this.tScores[i][j] = "N";   
                     }
                     temp = 0;
                     tempSp = 0;
                 }
             }
        }
    }

Any idea why Math.sqrt(0.0833) and Math.sqrt(1/12) would evaluate to different values?

The T score when I add the 1/24 and 1/24 value and take the sqrt keeps evaluating to zero but when I plug in the actual decimal it gives me the answer I would expect

Any ideas why this is occuring?

3 Answers3

1

1/12==0 as per integer division

Bing Wang
  • 1,445
  • 1
  • 5
  • 7
1

There's nothing wrong with Math.sqrt. You're passing it the number zero.

Math.sqrt(1/12)

1/12 is a division operation on two integers (namely, 1 and 12), so it's going to produce an integer result. 12 goes into 1 zero times, so the integer result is zero. Thus, your expression is

Math.sqrt(0)

Consider

Math.sqrt(1.0/12.0)
Silvio Mayolo
  • 24,199
  • 3
  • 34
  • 65
1
Math.sqrt(1/12d)

Cast 1/12 to a double by casting one operand to a double.

Spectric
  • 5,761
  • 2
  • 6
  • 27