Results 1 to 19 of 19
Thread: quadratic Formula Problem...
 11292014, 12:27 AM #1Member
 Join Date
 Nov 2014
 Posts
 9
 Rep Power
 0
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.
 11292014, 12:29 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 11292014, 07:18 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,423
 Blog Entries
 7
 Rep Power
 27
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 12012014, 06:38 PM #4Senior Member
 Join Date
 Sep 2014
 Location
 MA, USA
 Posts
 398
 Rep Power
 5
Re: quadratic Formula Problem...
I would check what the outcome is if you comment out the disposal of the scanner instances.
 12012014, 10:16 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: quadratic Formula Problem...
You only need a single Scanner instance for user input.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12022014, 06:52 AM #6Member
 Join Date
 Dec 2014
 Location
 Asian
 Posts
 10
 Rep Power
 0
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
 12022014, 07:15 AM #7Member
 Join Date
 Dec 2014
 Location
 Asian
 Posts
 10
 Rep Power
 0
Re: quadratic Formula Problem...
you know that a b, and c can be any value. so 4ac can be much bigger than b*b.
 12022014, 07:16 AM #8Member
 Join Date
 Dec 2014
 Location
 Asian
 Posts
 10
 Rep Power
 0
Re: quadratic Formula Problem...
try better data to avoid exception
 12032014, 06:09 PM #9Member
 Join Date
 Nov 2014
 Posts
 9
 Rep Power
 0
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?
 12032014, 06:43 PM #10Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: quadratic Formula Problem...
In what way? Did you make the changes Jos recommended?
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12032014, 08:08 PM #11Member
 Join Date
 Nov 2014
 Posts
 9
 Rep Power
 0
Re: quadratic Formula Problem...
Jim: I made changes enough so that it returns whether or not b*b4ac 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?
 12032014, 08:13 PM #12Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: quadratic Formula Problem...
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12042014, 12:20 AM #13Member
 Join Date
 Dec 2014
 Location
 Asian
 Posts
 10
 Rep Power
 0
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.
 12062014, 09:44 PM #14Member
 Join Date
 Nov 2014
 Posts
 9
 Rep Power
 0
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)); } } }
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.
 12062014, 10:09 PM #15Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: quadratic Formula Problem...
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12072014, 03:59 AM #16Member
 Join Date
 Nov 2014
 Posts
 9
 Rep Power
 0
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.
 12072014, 04:25 AM #17Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: quadratic Formula Problem...
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 nontrivial 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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 12072014, 04:39 AM #18Member
 Join Date
 Nov 2014
 Posts
 9
 Rep Power
 0
Re: quadratic Formula Problem...
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; 12072014 at 04:42 AM.
 12072014, 03:46 PM #19Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: quadratic Formula Problem...
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?
And finally, you only need a single scanner instance. They are reusable.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Quadratic equation help please.
By proPAGANda in forum New To JavaReplies: 3Last Post: 10092012, 01:36 PM 
need help with factor quadratic...
By frostkarrotor in forum New To JavaReplies: 3Last Post: 11302010, 12:28 AM 
program for a quadratic formula
By bbtgirl in forum New To JavaReplies: 9Last Post: 01302009, 06:06 PM 
Quadratic Equation
By jpnym15 in forum New To JavaReplies: 4Last Post: 11122008, 03:29 AM 
Quadratic GUI
By ryn21 in forum New To JavaReplies: 1Last Post: 10302008, 05:58 AM
Bookmarks