Vector Addition with small numbers giving NaN

• October 28th, 2010, 02:23 PM
Zula
Vector Addition with small numbers giving NaN
Code :

``` public void addVector(mVector v) { double rx = v.getXComponent()+getXComponent(); double ry = v.getYComponent()+getYComponent(); mag = Math.sqrt((rx*rx)+(ry*ry)); dir = Math.toDegrees(Math.atan(ry/rx)); } public double getXComponent() { return (mag*Math.cos(Math.toRadians(dir))); } public double getYComponent() { return (mag*Math.sin(Math.toRadians(dir))); }```

When adding an mVector with a mag and dir of 0 to an mVector with a mag of .735 and a dir of 90 I keep getting NaN results
• October 28th, 2010, 04:57 PM
Zula
Re: Vector Addition with small numbers giving NaN
Code :

```public class mVector { public double mag; public double dir; public mVector(double mag, double dir) { this.mag = mag; this.dir = dir; }   public void addVector(mVector v) { double rx = v.getXComponent()+getXComponent(); double ry = v.getYComponent()+getYComponent(); System.out.println("("+v.getXComponent()+", "+v.getYComponent()+")+("+getXComponent()+", "+getYComponent()+")"); mag = Math.sqrt((rx*rx)+(ry*ry)); dir = Math.toDegrees(Math.atan(ry/rx)); System.out.println(0*Math.cos(Math.toRadians(0))); }   public double getXComponent() { return (mag*Math.cos(Math.toRadians(dir))); } public double getYComponent() { return (mag*Math.sin(Math.toRadians(dir))); } public String toString() { return mag+"@"+dir; } }```

Just incase it wasn't clear, that code is in a class called mVector, and it is an instance of that class, with a mag of 0 and a dir of 0, that I call
• October 28th, 2010, 06:58 PM
copeg
Re: Vector Addition with small numbers giving NaN
I just quickly ran the code with those values and didn't get any NaN
• October 28th, 2010, 08:23 PM
Zula
Re: Vector Addition with small numbers giving NaN
Hah, okay, the problem was hat I was passing a 0 for magnitude of new vector...rather than .735 as predicted...uncaught loss of precision due to multiplying by a long perhaps..
• October 28th, 2010, 09:29 PM
Zula
Re: Vector Addition with small numbers giving NaN
Actually, that was NOT the problem. I put a println to show the value of v.mag was non-zero, and still not working, so I made a tester class, run this:
Code :

```public class mVectorTester {   /** * Creates a new instance of <code>mVectorTester</code>. */ public mVectorTester() { }   /** * @param args the command line arguments */ public static void main(String[] args) { mVector mv = new mVector(0,0); long delta = 10; double Ag = 49.0; double Ag2 = (Ag*delta)/1000; System.out.println(Ag2); long lastTime = System.currentTimeMillis(); for(int i = 0;i<10;i++) { delta = System.currentTimeMillis() - lastTime; lastTime = System.currentTimeMillis(); Ag2 = (Ag*delta)/1000; mv.addVector(new mVector(Ag2, 90));   System.out.println(Ag2); try{ Thread.sleep(10); } catch(InterruptedException e) {} } } /** * mVector.java * * A Vector describing either a velocity or a force * mag - The magnitude of the vector in m/s * dir - The direction of the vector in degrees * @author Ralph Landon * @version 1.00 2010/10/28 */ private static class mVector { public double mag; public double dir; public mVector(double mag, double dir) { this.mag = mag; this.dir = dir; }   public void addVector(mVector v) { double rx = v.getXComponent()+getXComponent(); double ry = v.getYComponent()+getYComponent(); System.out.println(v.mag); System.out.println("("+v.getXComponent()+", "+v.getYComponent()+")+("+getXComponent()+", "+getYComponent()+")"); mag = Math.sqrt((rx*rx)+(ry*ry)); dir = Math.toDegrees(Math.atan(ry/rx)); System.out.println(0*Math.cos(Math.toRadians(0))); }   public double getXComponent() { return (mag*Math.cos(Math.toRadians(dir))); } public double getYComponent() { return (mag*Math.sin(Math.toRadians(dir))); } public String toString() { return mag+"@"+dir; } } }```