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.