我的代码是这样的:开始求出的数组a,b 用System.out.println(**)输出都正常,然后我这样写就错了:
                   for (int i = 0; i < numPos; i++)
{
double rowSumA = 0.0D;
for (int j = 0; j < numPos; j++)
{
rowSumA += a[i][j];
}
for (int j = 0; j < numPos; j++)
{
a[i][j] = a[i][j] / rowSumA;
}
double rowSumB = 0.0D;
for (int j = 0; j < numWords; j++)
{
rowSumB += b[i][j];
}
for (int j = 0; j < numWords; j++)
{
b[i][j] = b[i][j] / rowSumB;
}
}然后我在输出a,b时,就出现很多NAN,是什么原因啊?

解决方案 »

  1.   

    rowSumA或者rowSumB为零,产生的结果是NAN,而对于整形则会抛出异常。
      

  2.   


    public class DivideTest { /**
      * @param args
      */
     public static void main(String[] args) {
      DivideTest dt = new DivideTest();
      
      // 整数
      dt.testInt(0, 0);
      dt.testInt(1, 0);
      dt.testInt(-1, 0);
      
      // 长整形
      dt.testLong(0, 0);
      dt.testLong(1, 0);
      dt.testLong(-1, 0);
      
      // 浮点
      dt.testFloat(0, 0);
      dt.testFloat(1, 0);
      dt.testFloat(-1, 0);
      
      // 双精度
      dt.testDouble(0, 0);
      dt.testDouble(1, 0);
      dt.testDouble(-1, 0);
      
      System.out.println("12>(0.0/0.0) "+(12>(0.0/0.0)));
      System.out.println("12>(0.1/0.0) "+(12>(0.1/0.0)));
      System.out.println("12<(0.0/0.0) "+(12<(0.0/0.0)));
      System.out.println("12<(0.1/0.0) "+(12<(0.1/0.0)));
      
      System.out.println("12==(0.0/0.0) "+(12==(0.0/0.0))); } private void testInt(int dividend, int divisor){
      try {
       System.out.println("testInt: "+dividend+"/"+divisor+" = "+dividend/divisor);
      } catch (RuntimeException e) {
       e.printStackTrace();
      }
     }
     
     private void testLong(long dividend, long divisor){
      try {
       System.out.println("testLong: "+dividend+"/"+divisor+" = "+dividend/divisor);
      } catch (RuntimeException e) {
       e.printStackTrace();
      }
     }
     
     private void testDouble(double dividend, double divisor){
      try {
       System.out.println("testDouble: "+dividend+"/"+divisor+" = "+dividend/divisor);
      } catch (RuntimeException e) {
       e.printStackTrace();
      }
     }
     
     private void testFloat(float dividend, float divisor){
      try {
       System.out.println("testFloat: "+dividend+"/"+divisor+" = "+dividend/divisor);
      } catch (RuntimeException e) {
       e.printStackTrace();
      }
     }}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/longaway/archive/2006/03/13/622904.aspx
      

  3.   

    a[i][j] = a[i][j] / rowSumA是不是除数是0