Florian Pellegrin - 6 months ago 42

Java Question

We are trying to get the cos value between v and u but we are getting results much higher than 1 or lesser than 0

Where :

`vx = in.nextInt(); // x speed of your pod`

vy = in.nextInt(); // y speed of your pod

int ux = nextCheckPointIdX - x;

int uy = nextCheckPointIdY - y;

Here is the formula :

`double cos = (vx*ux + vy*uy) / ( Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) + Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2)) );`

Do you find any errors in the previous line ?

Answer

The denominator was having the problem.

```
int num = (vx*ux + vy*uy);
double den = (Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) * (Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2))) );
double cos = num / den;
System.out.println(cos);
System.out.println(Math.acos(cos));
```