Results 1 to 19 of 19
  1. #1
    superrm11 is offline Member
    Join Date
    Nov 2014
    Posts
    9
    Rep Power
    0

    Default quadratic Formula Problem...

    So, I have a question that's been bugging me for about a month now.

    I've been working on a program that solves the quadratic formula for me, and I'm not able to figure out just what I've done wrong. Whenever I enter the numbers, then the answers just come out as NaN, which I'm assuming stands for Not a Number. I have absolutely no idea why its doing this, or what's wrong with my code. Here it is:

    Java Code:
    package allEquations;
    import java.util.*;
    class AllEquations{
    	public static void main(String[] args) {
    		System.out.print("Enter a: ");
    		Scanner keyInput = new Scanner(System.in);
    		int a = keyInput.nextInt();
    		keyInput =  null;
    		System.out.println();
    //declaring a
    		System.out.print("Enter b: ");
    		Scanner keyInput1 = new Scanner(System.in);
    		int b = keyInput1.nextInt();
    		keyInput1 = null;
    		System.out.println();
    //declaring b
    		System.out.print("Enter c: ");
    		Scanner keyInput2 = new Scanner(System.in);
    		int c = keyInput2.nextInt();
    		keyInput2 = null;
    		System.out.println();
    //declaring c
    		double y = ((-b + Math.sqrt((b * b) - (4 * a * c))) / (2 * a));
    		double y1 = ((-b - Math.sqrt((b * b) - (4 * a * c))) / (2 * a));
    		System.out.println("y = " + y);
    		System.out.println("y = " + y1);
    //Trying to use the quadratic formula, but not working...
    	}
    }

    I'm open to all suggestions.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    Please include the test data you are using. Are you checking to ensure the b*b - 4*a*c is >= 0? Taking square root of negative numbers can cause problems.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: quadratic Formula Problem...

    Also read this: http://people.csail.mit.edu/bkph/art...Quadratics.pdf for a numerically more stable solution of the quadratic system; the text book method can cause large errors for certain values of a, b and c.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: quadratic Formula Problem...

    I would check what the outcome is if you comment out the disposal of the scanner instances.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    You only need a single Scanner instance for user input.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    janissantony is offline Member
    Join Date
    Dec 2014
    Location
    Asian
    Posts
    10
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    Perhaps the formula gives complex numbers as result. I.e. with imaginary number which will throw the exception in your case

  7. #7
    janissantony is offline Member
    Join Date
    Dec 2014
    Location
    Asian
    Posts
    10
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    you know that a b, and c can be any value. so 4ac can be much bigger than b*b.

  8. #8
    janissantony is offline Member
    Join Date
    Dec 2014
    Location
    Asian
    Posts
    10
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    try better data to avoid exception

  9. #9
    superrm11 is offline Member
    Join Date
    Nov 2014
    Posts
    9
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    Thanks for all the replies.
    Beji2505:I set an if then statement to check whether (b*b)-4*a*c is greater than 0. It works for now, but I need a better solution.

    Janissantony: I never actually thought of that but it is true. Do you know of any variable types or imports I could use for imaginary numbers?

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    Quote Originally Posted by superrm11 View Post
    It works for now, but I need a better solution.
    In what way? Did you make the changes Jos recommended?

    Quote Originally Posted by superrm11 View Post
    Do you know of any variable types or imports I could use for imaginary numbers?
    There are no complex variables. But you could create complex objects which represent them. You can read up on complex variables and see how to do the basic operations. The class would have a real and imaginary part and the supporting methods.

    But in this case you don't need them. Complex numbers are simply numbers in the form of a + bi or a - bi where i is the square root of negative 1. So just check to see if the number under the radical sign is negative. If so, factor out -1 and compute the parts separately. Then simply suffix i to the imaginary part.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    superrm11 is offline Member
    Join Date
    Nov 2014
    Posts
    9
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    Jim: I made changes enough so that it returns whether or not b*b-4ac is greater than zero. How would I factor out i though? Could I just divide by the square root of -1 and if it is less than zero (before I divide) then tack on the i?

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    Quote Originally Posted by superrm11 View Post
    How would I factor out i though? Could I just divide by the square root of -1 and if it is less than zero (before I divide) then tack on the i?
    You don't factor out i, you factor out -1. But you do end up tacking on i.

    Say you want to take the square root of -81. I'll use sr as a square root function.
    So sr(-81) = sr(81) * sr(-1) = 9i.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    janissantony is offline Member
    Join Date
    Dec 2014
    Location
    Asian
    Posts
    10
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    I agree with @jim829 , you may need to create a class to represent complex numbers. I am not sure if there is existing library for this.

  14. #14
    superrm11 is offline Member
    Join Date
    Nov 2014
    Posts
    9
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    UPDATE:

    Okay, so I have just fixed the code (mostly) and just needs a few things worked out. Here it is:

    Java Code:
    package allEquations;
    import java.util.*;
    class AllEquations{
    	public static void main(String[] args) {
    		System.out.print("Enter a: ");
    		Scanner keyInput = new Scanner(System.in);
    		int a = keyInput.nextInt();
    		keyInput =  null;
    		System.out.println();
    //declaring a
    		System.out.print("Enter b: ");
    		Scanner keyInput1 = new Scanner(System.in);
    		int b = keyInput1.nextInt();
    		keyInput1 = null;
    		System.out.println();
    //declaring b
    		System.out.print("Enter c: ");
    		Scanner keyInput2 = new Scanner(System.in);
    		int c = keyInput2.nextInt();
    		keyInput2 = null;
    		System.out.println();
    //declaring c
    		double part1 = (b * b) - 4 * a * c;
    		if (part1 >= 0){
    			double y = ((-b + Math.sqrt((b * b) - (4 * a * c))) / (2 * a));
    	        double y1 = ((-b - Math.sqrt((b * b) - (4 * a * c))) / (2 * a));
    	        System.out.println("y = " + y);
    	        System.out.println("y = " + y1);
    		} else {
    			part1 = part1 * -1;
    			System.out.println("y = " + (-b) + "+-" + " i " + "sqrt( " + part1 + " )" + "/" + (2 * a));
    	        	
    	    }
    	}
    }
    So, for example, when I input:

    a = 543
    b = 1
    c = 36


    then it outputs:

    y = -1+- i sqrt( 78191.0 )/1086

    The only trouble I have is that I don't know how to simplify the square root, and I don't know how to use characters for the square root symbol.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    Quote Originally Posted by superrm11 View Post
    then it outputs:

    y = -1+- i sqrt( 78191.0 )/1086

    The only trouble I have is that I don't know how to simplify the square root, and I don't know how to use characters for the square root symbol.
    Why are you printing it out in symbolic form? Just use the actual values. For example:

    Instead of printing this:

    y = -10 + sqrt(-81)/3

    print this:

    y = -10 + 3i

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    superrm11 is offline Member
    Join Date
    Nov 2014
    Posts
    9
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    Jim:
    I cant print it out like that, because that would be the wrong value. you see, when you say:

    y = -10 + sqrt(-81)/3

    that would imply:

    y = (-10 + sqrt(-81)) / 3

    and therefore mean:

    y = (-10 / 3) + (sqrt(-81) / 3)

    because of the distributive property. So that means you would be off by around -1.33, and I want exact answers.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    Quote Originally Posted by superrm11 View Post
    Jim:
    I cant print it out like that, because that would be the wrong value. you see, when you say:

    y = -10 + sqrt(-81)/3

    that would imply:

    y = (-10 + sqrt(-81)) / 3
    Nope. Addition and subtraction have lower precedence (even in mathematics) than multiplication and division. So sans parentheses, multiplication and division take place prior to addition or subtraction.

    So a + b / c is the same as a + (b / c).

    But more to the point. You apparently want to use the square root radical sign but you can't because you are printing in text. So your first method will suffice. To simplify what is in the sqrt() function is non-trivial and would require a lot of factoring and checking. So unless simplification is a firm requirement, I would leave it as you had it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    superrm11 is offline Member
    Join Date
    Nov 2014
    Posts
    9
    Rep Power
    0

    Default Re: quadratic Formula Problem...

    Quote Originally Posted by jim829 View Post
    Nope. Addition and subtraction have lower precedence (even in mathematics) than multiplication and division. So sans parentheses, multiplication and division take place prior to addition or subtraction.

    So a + b / c is the same as a + (b / c).

    But more to the point. You apparently want to use the square root radical sign but you can't because you are printing in text. So your first method will suffice. To simplify what is in the sqrt() function is non-trivial and would require a lot of factoring and checking. So unless simplification is a firm requirement, I would leave it as you had it.

    Regards,
    Jim
    I was just wondering whether I could use a symbol for the square root instead of sqrt(), and I did. I could just place a √ with the character map on windows and print it. And one more thing before I close this thread, does anyone know of how to make my .class file into an executable jar file, so when I run it, it opens the command prompt?
    Last edited by superrm11; 12-07-2014 at 04:42 AM.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: quadratic Formula Problem...

    Quote Originally Posted by superrm11 View Post
    I was just wondering whether I could use a symbol for the square root instead of sqrt(), and I did. I could just place a √ with the character map on windows and print it.
    I thought about suggesting that but didn't for several reasons. The first is that it may depend on the current font. The other is that there is no top bar to delineate what is actually under the radical. So it can get confusing if there are other values to its right since it won't be obvious if they are part of the square root function or not. Using parentheses could mitigate this.

    And one more thing before I close this thread, does anyone know of how to make my .class file into an executable jar file, so when I run it, it opens the command prompt?
    You can read about jar files here. Using JAR Files: The Basics (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files). I would think that if the jar prompts for user input, the console would appear. If not, you could always use a dialog box.

    And finally, you only need a single scanner instance. They are reusable.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Quadratic equation help please.
    By proPAGANda in forum New To Java
    Replies: 3
    Last Post: 10-09-2012, 01:36 PM
  2. need help with factor quadratic...
    By frostkarrotor in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 12:28 AM
  3. program for a quadratic formula
    By bbtgirl in forum New To Java
    Replies: 9
    Last Post: 01-30-2009, 06:06 PM
  4. Quadratic Equation
    By jpnym15 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 03:29 AM
  5. Quadratic GUI
    By ryn21 in forum New To Java
    Replies: 1
    Last Post: 10-30-2008, 05:58 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •