简体   繁体   中英

NaN output, i can't find out why

I am creating code to help myself with some homework, but there is a problem with class 'C'. It keeps giving me NaN output, can You look at it and tell me what am I doing wrong?

I double check it and found out problem lies it this line:

C[i][j] = C[i][j] * (Math.sqrt(((Math.pow(U[i][j],2)/Math.pow(I[i][j],2)))-Math.pow(R[i],2)));

Main -


    public class Main {

        public static void main(String[] args) {

            Wzory wzor = new Wzory();
            wzor.scan();
            wzor.xU();
            wzor.xI();
            wzor.C();

        }

    }

package FizA;
import java.util.Scanner;
import java.lang.Math;

public class Wzory {
    //przypisane
    double[] f = new double[2],xf = new double[2],R = new double[2],xR = new double[2];
    double[][] U = new double[2][4],I = new double[2][4];
    double pi=3.1415;
    //obliczone
    double[][] xU = new double[2][4], xI = new double[2][4], C = new double[2][4], xC = new double[2][4];


    public void scan()
    {
        Scanner scan = new Scanner(System.in);
        for(int i=0; i<2; i++)
        {
            System.out.println("Podaj 'f':");
            f[i] = scan.nextDouble();
            System.out.println("Podaj 'xf':");
            xf[i] = scan.nextDouble();
            System.out.println("Podaj 'R':");
            R[i] = scan.nextDouble();
            System.out.println("Podaj 'xR':");
            xR[i] = scan.nextDouble();
            for(int j=0; j<4; j++)
            {
                System.out.println("Podaj 'U':");
                U[i][j] = scan.nextDouble();
                System.out.println("Podaj 'I':");
                I[i][j] = scan.nextDouble();
            }
        }
        scan.close();
    }

    public void xU()
    {
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<4; j++)
            {
                xU[i][j] = 0.005 * U[i][j];
                xU[i][j] = xU[i][j] + 0.003;    
                System.out.println("xU-"+i+","+j+"="+xU[i][j]);
            }
        }

    }

    public void xI()
    {
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<4; j++)
            {
                xI[i][j] = 0.015 * I[i][j];
                xI[i][j] = xI[i][j] + 0.000003;
                System.out.println("xI"+i+","+j+"="+xI[i][j]);
            }
        }
    }

    public void C()
    {
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<4; j++)
            {
                C[i][j] = 2*pi*f[i];
                C[i][j] = C[i][j] * (Math.sqrt(((Math.pow(U[i][j],2)/Math.pow(I[i][j],2)))-Math.pow(R[i],2)));
                C[i][j] = Math.pow(C[i][j],-1);
                System.out.println("C"+i+","+j+"="+C[i][j]);
            }
        }
    }



}

You are taking a square root of this, Math.pow(U[i][j],2)/Math.pow(I[i][j],2)))-Math.pow(R[i],2)

Your NaN comes from:

1) the Math.sqrt method

2) and the minus Math.pow(R[i],2), since negative numbers don't have a square root, a square is either a 0 or a positive number.

Example:

When U[i][j] to the power of 2 / I[i][j] to the power of 2, minus R[i] to the power of 2, gives a negative number, your Math.sqrt of this negative number will give an NaN, even though you multiply it with C[i][j] and store it back in C[i][j] and print it.

That's how you get an NaN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM