Division between double values returns 0

2

I am writing a Java code consisting of several simple mathematical operations (divisions and multiplications). It is a problem of Physics.

It turns out that when calculating certain operations, with values already obtained, the results of these give 0. I do not understand why this error occurs, because it is simple operations.

public Calculos(int alfa, double Vo) {
        mE = 9.11 * (Math.pow(10, -31));
        q = 6.67 * (Math.pow(10, -19));
        E = 3.5 * (Math.pow(10, 3));

        a = 0;
        Vox = 0;
        Voy = 0;
        t = 0;
        x = 0;
        y = 0;
        tTotal = 0;

        this.alfa = alfa;
        this.Vo = Vo;
    }

    public void mostrarDatos() {
            System.out.println("Angulo de lanzamiento: " + alfa + "º");
            System.out.println("Velocidad inicial: " + Vo + " m/s");
            System.out.println("Vox = " + Vo + "·cos " + alfa + " = " + String.format("%.2f", Vo * (Math.cos(Math.toRadians(alfa)))));
            System.out.println("Voy = " + Vo + "·sen " + alfa + " = " + String.format("%.2f", Vo * (Math.sin(Math.toRadians(alfa)))));
            Vox = Vo * (Math.cos(Math.toRadians(alfa)));
            Voy = Vo * (Math.sin(Math.toRadians(alfa)));
        }

        public void calcularAceleracion() {
            System.out.println("Aceleracion");
            System.out.println("a = F/mE = q·E/mE = " + String.format("%.2f", (q * E) / mE));
            a = (q * E) / mE;
        }

        public void calcularAlturaMaxima() {
            System.out.println("Despejando, tiempo en alcanzar y maxima = " + String.format("%.2f", (Voy / a)) + " s");
            t = Voy / a;
            System.out.println("Por tanto la altura maxima y = " + String.format("%.2f", (Voy * t - 0.5 * a * (Math.pow(t, 2)))) + " m");
            y = (Voy * t - 0.5 * a * (Math.pow(t, 2)));
            System.out.println("La coordenada x de la altura maxima x = Vox·t = " + Vox + "·" + t + " = " + String.format("%.2f", Vox * t) + " m");

        }

The execution is as follows:

Angulo de lanzamiento: 45º
Velocidad inicial: 5000000.0 m/s
Vox = 5000000.0·cos 45 = 3535533,91
Voy = 5000000.0·sen 45 = 3535533,91
Aceleracion
a = F/mE = q·E/mE = 2562568605927552,00
Despejando, tiempo en alcanzar y maxima = 0,00 s
Por tanto la altura maxima y = 0,00 m
La coordenada x de la altura maxima x = Vox·t = 3535533.905932738·1.3796836103254332E-9 = 0,00 m

All data is of type double except the alpha value, which is int.

    
asked by DDN 04.03.2017 в 12:52
source

1 answer

1

You may need more capacity when displaying decimals, for example you can look at this:

  • String.format ("% .50f"
public void calcularAlturaMaxima() {

//..

System.out.println("Despejando, tiempo en alcanzar y maxima = " + String.format("%.50f", (Voy / a)) + " s");

//..

System.out.println("Por tanto la altura maxima y = " + String.format("%.50f", (Voy * t - 0.5 * a * (Math.pow(t, 2)))) + " m");

//..

I have calculated with 1, 1, for alfa and Vo and the result is this:

Angulo de lanzamiento: 1º
Velocidad inicial: 1.0 m/s
Vox = 1.0·cos 1 = 1.00
Voy = 1.0·sen 1 = 0.02
Aceleracion
a = F/mE = q·E/mE = 2562568605927552.00
Despejando, tiempo en alcanzar y maxima = 0.00000000000000000681051285687097000000000000000000 s
Por tanto la altura maxima y = 0.00000000000000000005942991921222852000000000000000 m
La coordenada x de la altura maxima x = Vox·t = 0.9998476951563913·6.81051285687097E-18 = 0.00 m

Which is:

Angulo de lanzamiento: 1º
Velocidad inicial: 1.0 m/s
Vox = 1.0·cos 1 = 1.00
Voy = 1.0·sen 1 = 0.02
Aceleracion
a = F/mE = q·E/mE = 2562568605927552.00
Despejando, tiempo en alcanzar y maxima = 0.00 s
Por tanto la altura maxima y = 0.00 m
La coordenada x de la altura maxima x = Vox·t = 0.9998476951563913·6.81051285687097E-18 = 0.00 m

for:

  • String.format ("%. 2f"
public void calcularAlturaMaxima() {

//..

System.out.println("Despejando, tiempo en alcanzar y maxima = " + String.format("%.2f", (Voy / a)) + " s");

//..

System.out.println("Por tanto la altura maxima y = " + String.format("%.2f", (Voy * t - 0.5 * a * (Math.pow(t, 2)))) + " m");

//..

Then in this example (and certainly in many) it's really not that of, a result of zero, it's simply that for the precision that you determine in format...%.2f , only those (0.00 and 0.00) are shown m).

    
answered by 04.03.2017 / 13:34
source