Welcome to Dream.In.Code
Become a Java Expert!

Join 149,609 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,886 people online right now. Registration is fast and FREE... Join Now!




Quadratic Formula Help

 
Reply to this topicStart new topic

Quadratic Formula Help, Return NaN error with certain coefficients

bulletb1331
13 Sep, 2007 - 03:20 PM
Post #1

New D.I.C Head
*

Joined: 13 Sep, 2007
Posts: 4


My Contributions
Hey Everyone, I'm new to the forum, I'm having a problem with a Java program we were assigned for homework. It's only my second week in the class so I'm a beginner to the whole language. Here's the code I've written so far:

CODE
/*
* HW2b
* Robert Durning
*/

// Quadratic Equation Solver
// Solve for x for equations in form ax^2 + bx + c = 0

import java.util.*;

public class QuadraticSolver
{
    public static void main(String[] args)
    {
        System.out.println("I will solve quadratic equations for you.");
        System.out.println("Enter in coefficients a, b and c:");
        
        Scanner keyboard = new Scanner(System.in);
        
        double a, b, c;
        a = keyboard.nextDouble();
        b = keyboard.nextDouble();
        c = keyboard.nextDouble();
        
        double x1 = ((-b + Math.sqrt(b*b + 4*a*c))/(2*a));
        double x2 = ((-b - Math.sqrt(b*b + 4*a*c))/(2*a));
        
        System.out.println("The answer is: x = " + x1 + " and x = " + x2);
    }
}



The program works for most coefficients but my professor had us try it with these coefficients a = 2, b = -1.2, and c = -6.3 and we should get answers around -1.5 and 2.1 but i return Not a number everytime. If anyone has any suggestions that would be great.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Quadratic Formula Help
13 Sep, 2007 - 03:33 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi as far as I can remember the quadratic formula is not like this:
QUOTE
double x1 = ((-b + Math.sqrt(b*b + 4*a*c))/(2*a));
double x2 = ((-b - Math.sqrt(b*b + 4*a*c))/(2*a));


it is like this:
CODE

double x1 = ((-b + Math.sqrt(b*b - 4*a*c))/(2*a));
double x2 = ((-b - Math.sqrt(b*b - 4*a*c))/(2*a));


and please don't start other threads for the same problem. This is a forum and your question, may be answered sooner or later.
User is offlineProfile CardPM
+Quote Post

The ArchiTECT
RE: Quadratic Formula Help
17 Sep, 2007 - 03:54 AM
Post #3

New D.I.C Head
*

Joined: 13 Jul, 2006
Posts: 1


My Contributions
Ey,

Of course the formula was wrong, but...

If you would fill in these numbers in the formula and do some head calculations you will immediately notice that you were taking the square root of a negative number.

So unless you are working in the complex space...


Sincerly Yours,
Archi.

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Quadratic Formula Help
17 Sep, 2007 - 04:14 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(The ArchiTECT @ 17 Sep, 2007 - 04:54 AM) *

Ey,

Of course the formula was wrong, but...

If you would fill in these numbers in the formula and do some head calculations you will immediately notice that you were taking the square root of a negative number.

So unless you are working in the complex space...


Sincerly Yours,
Archi.


well lets do that, shall we:
the numbers are as follows:

QUOTE
a = 2, b = -1.2, and c = -6.3


so lets do some math here:(using the correct formula!)

x1 = ((-b + Math.sqrt(b*b - 4*a*c))/(2*a));

x1 = ((-(-1.2) + Math.sqrt((-1.2)*(-1.2) - 4*2*(-6.3)))/(2*2));

x1 = (1.2 + Math.sqrt(1.44 - (-50.4)))/(2*2));

x1 = (1.2 + Math.sqrt(51.84)/(2*2));

x1 = (1.2 + 7.2)/(2*2);

x1 = (8.4)/(2*2);

x1 = (8.4)/(4);

x1 = 2.1;
_____________________________________________________

x2 = ((-b - Math.sqrt(b*b - 4*a*c))/(2*a));

x2 = ((-(-1.2) - Math.sqrt((-1.2)*(-1.2) - 4*2*(-6.3)))/(2*2));

x2 = (1.2 - Math.sqrt(1.44 - (-50.4)))/(2*2));

x2 = (1.2 - Math.sqrt(51.84)/(2*2));

x2 = (1.2 - 7.2)/(2*2);

x2 = (-6)/(2*2);

x2 = (-6)/(4);

x2 = -1.5;
_____________________________________________________

please correct me if I'm wrong
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Quadratic Formula Help
17 Sep, 2007 - 07:22 AM
Post #5

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Math.sqrt(arg0) where arg0 is negative returns NaN, and you can test for that. Alternately, you can calculate the discriminant separately and catch negative values beforehand.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:06AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month