Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Vector Addition with small numbers giving NaN

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Vector Addition with small numbers giving NaN

            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


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Vector Addition with small numbers giving NaN

    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
    mv.addVector(new mVector(.735,90));

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Vector Addition with small numbers giving NaN

    I just quickly ran the code with those values and didn't get any NaN

  4. #4
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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..

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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:
    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;
    	}
    }
    }

Similar Threads

  1. [SOLVED] Giving Java the name of a file to be read via command line?
    By lavloki in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 14th, 2010, 10:07 AM
  2. Replies: 0
    Last Post: March 5th, 2010, 12:32 AM
  3. Small Project
    By 3XiLED in forum Paid Java Projects
    Replies: 7
    Last Post: March 1st, 2010, 07:35 AM
  4. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 10:26 AM
  5. [SOLVED] Please Review My Code (Long Integer Addition)
    By Saradus in forum Java Theory & Questions
    Replies: 10
    Last Post: July 4th, 2009, 12:55 PM