NaN, Java trouble

155 views Asked by At

I am trying to divide two full double values (i.e. double variables with all the memory filled up with a high precision number). Moreover, the result of the operation will be allocated in a double variable.

When I do the process, this one returns me NaN.

I read in some place before that two full double values when multiplied or divided between themselves will result in a high memory value, one that cannot be allocated in a double variable.

Am I right?

How could I solve this trouble?

public class CoeficienteAngular {

    public static double coefAngularImpar(    int x/*Coeficiente 
    da longarina a qual se analiza*/,         double y/*Distância 
    entre as longarinas*/,                     double z/*Somatorio*/){

        double beta;
        double betaPrim;

        betaPrim = (double) x/y;
        beta = betaPrim/z;

        return beta;
    }

    public static double coefAngularPar(    int x/*Coeficiente 
    da longarina que se analiza*/,             double y/*Distância 
    entre as longarinas*/,                     double z/*Somatório*/){

        double numerador, denominador, fatorCorrecao;
        double beta;

        int moduloL = Math.abs(x);

        numerador = (double)(moduloL-1)+(double)(1/2);
        denominador = y*z;

        fatorCorrecao = 1;//Inicializando a variável
        if(x>=0){
            fatorCorrecao = 1;
        }else if(x<0){
            fatorCorrecao = -1;
        }

        beta = fatorCorrecao*(numerador/denominador);
        return beta;
    }

}
1

There are 1 answers

0
Kraal On BEST ANSWER

Don't use doubles. To avoid precision loss, use BigDecimal instead (see Javadoc). You can also read this post where people explained perfectly the reasons for using BigDecimal.