Math Problem
I'm doing this project where I am writing a program that does math equations but for some reason when I enter certain numbers for my coefficients instead of the answer being a number I get the answer "NaN" and sometimes the answer is "-Infinity". What's going on? I asked my instructor "Google" but couldn't find anything helpful.
~Kaeloc
[354 byte] By [
Kaeloca] at [2007-11-26 20:01:27]

Post some code. You aren't taking the square roots of negative numbers, are you?
Please read http://en.wikipedia.org/wiki/Floating_point#Dealing_with_exceptional_cases
NaN is short for "not a number"
what you get when the result of a calculation cannot be presented using a floating point number
for example 0.0 divided by 0.0 or logarithm of -1
Infinities
- arise when the absolute value of a result of a calculation is greater than the maximum allowed
for example 1 divided by 0.0 or logarithm of 0
CapMorganOn the Puyo Puyo thread your image only sent the clicker? clickee? to a logout screen if I remember correctly. http://forum.java.sun.com/thread.jspa?threadID=523937&tstart=0jsalonenThanks I will read up on it.~Kaeloc
> CapMorgan...What? Did you click on it?
How can I get x1 and x2 to display the calculations correctly? If you enter 1, 2, and 3 for a, b, and c the program will display for the answer NaN and NaN. What do I need to do to properly display the answer? Does it have something to do with x1 and x2 being double? Links to guides or walkthoughs also welcome...
~Kaeloc
import cs1.Keyboard;
class QuadraticEquation {
public static void main(String[] args)
throws java.io.IOException
{
QuadraticEquation quadraticequation = new QuadraticEquation();
double a, b, c;
double x1, x2;
System.out.print("Enter a:");
System.out.flush();
a = cs1.Keyboard.readDouble();
System.out.print("Enter b:");
System.out.flush();
b = cs1.Keyboard.readDouble();
System.out.print("Enter c:");
System.out.flush();
c = cs1.Keyboard.readDouble();
x1 = ( -b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = ( -b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
if (x1 == x2) {
System.out.println("Solutions to equation is " + x1);
}
else {
System.out.println("Solutions to equation are " + x1 + " and " + x2);
}
}
}
Well, what do you get when you solve the equation by hand?We have x?+ 2x + 3 = (x + 1)?+ 2 which is greater than or equal to 2 for all values of x. Thus there are no solutions for x?+ 2x + 3=0; in other words the solutions are not numbers. Therefore it prints NaN.
I reckon it should print "i"!!It would be cool if your program can do that =)
before calculating x1 & x2 put a check condition D = b*b - 4*a*c >= 0 i.e. it should first check that the roots are realotherwise it should give a message that the roots are imaginary...
You should evaluate b * b - 4 * a * c first. You get three cases: Postive, zero or negative.
1. If it's zero you don't have to evaluate the square root (because it will be zero anyway and x1 and x2 will be equal).
2. If it's negative you know you got an imaginary solution. Before you take the root you first make it a positive number by negating it.
3. If it's positive you have the real solution you seem to think you always get because you slept on the math lectures.
If you want to become a programmer at least learn rudimentary math or be a sucker for the rest of your life.
minor nitpick @the last two replies: s/imaginary/complex. Only iff b == 0the roots will be imaginary for b*b-4*a*c < 0.kind regards,Jos
Right so I understand all of that, but if a=0, b=3, and c=6 then the answer is undefined or Infinity right? You can't divide by zero.~Kaeloc
Like I understand that you can't divide by zero, it's just that one of my instructor's examples said that if a=0, b=3, and c=6 then the answer would be -2 so I am a little confused. And as far as the posts by Guest above saying that I need to learn rudimentary math skills, I am in 10th grade in highschool and think that programming is fun. The course I am taking is a college level java course. Like everything else, there is a learning curve...so be patient with me.
~Kaeloc
Here is my code as you can see I have tried to fix any errors and I feel like I have finished the assignment, but there is still that example from my instructor that has me worried. Basically I seek moral support and possibly clarification if I'm wrong about a=0, b=3, c=6 with x=-2 in the equation.
~Kaeloc
import cs1.Keyboard;
class QuadraticEquation {
public static void main(String[] args)
throws java.io.IOException
{
double a, b, c;
double x1, x2, x12, x22;
System.out.print("Enter a:");
System.out.flush();
a = cs1.Keyboard.readDouble();
System.out.print("Enter b:");
System.out.flush();
b = cs1.Keyboard.readDouble();
System.out.print("Enter c:");
System.out.flush();
c = cs1.Keyboard.readDouble();
x1 = (b * b - 4 * a * c);
x2 = (b * b - 4 * a * c);
x12 = ( -b + Math.sqrt(x1)) / (2 * a);
x22 = ( -b - Math.sqrt(x2)) / (2 * a);
if (x1 < 0 && x2 < 0)
{
System.out.println("Solution to equation " + a + "x^2 + " + b + "x + " + c + " = 0"+ " is not real");
}
else if (x12 == x22)
{
System.out.println("Solution to equation " + a + "x^2 + " + b + "x + " + c + " = 0"+ " is " + x12);
}
else if (a == 0)
{
System.out.println("Error, cannot divide by zero");
}
else
{
System.out.println("Solutions to equation " + a + "x^2 + " + b + "x + " + c + " = 0 are " + x12 + " and " + x22);
}
}
}
> Like I understand that you can't divide by zero, it's
> just that one of my instructor's examples said that
> if a=0, b=3, and c=6 then the answer would be -2 so I
> am a little confused.
Forget what your instructor said. Plug the numbers in and see for yourself what you get.
jverda at 2007-7-21 17:48:59 >

Right ok, when I do the calculation in my head and I solve the problem this way a * x^2 + b * x + c = 0 with a=0, b=3, c=6 then it is easy to find x=-2, but when you do using the quadratic equation formula,
x1 = (-b + (sqrt)b^2-4ac)/2a
then the answer is undefined because if a=0 and you can't divide by 0.
And I wouldn't know how to code to solve for x using the first formula...
~Kaeloc
import java.util.Scanner;
class QuadraticEquation {
public static void main(String[] args)
throws java.io.IOException
{
// QuadraticEquation quadraticequation = new QuadraticEquation();
double a, b, c;
double x1, x2;
boolean correct=false;
Scanner s = new Scanner(System.in);
System.out.print("Enter a:");
a = s.nextDouble();
// a = cs1.Keyboard.readDouble();
System.out.print("Enter b:");
b = s.nextDouble();
System.out.print("Enter c:");
c = s.nextDouble();
if( b * b - 4 * a * c > 0)
{
correct = true;
}
if(correct)
{
x1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 * a;
x2 = ( -b - Math.sqrt(b * b - 4 * a * c)) / 2 * a;
System.out.println("Solutions to equation are " + x1 + " and " + x2);
}
else
{
System.out.println("hurray its an imaginary number ");
}
}
}
> Right ok, when I do the calculation in my head and I
> solve the problem this way a * x^2 + b * x + c = 0
> with a=0, b=3, c=6 then it is easy to find x=-2, but
> when you do using the quadratic equation formula,
>
> x1 = (-b + (sqrt)b^2-4ac)/2a
>
> then the answer is undefined because if a=0 and you
> can't divide by 0.
> And I wouldn't know how to code to solve for x using
> the first formula...
You don't use the quadratic formula when a is zero. There's a single solution: x = -c/b (if b != 0).
jverda at 2007-7-21 17:49:00 >

Hi Dudes
If i have to do same kind of work I will preffer to make a complex class and then start working on the same issues.
This will help me in finding out the exact solutions always.
Either real or imaginary.
This class must have a functions to find out squares and all the complex conditions
Thanks
fastmikeAh, I see, I didn't even think to use boolean.jverdthanks, that was so simple I can't believe I didn't see it.Let me work on it a little more...~Kaeloc
Thinking about it...why even use boolean? Couldn't I just say if disciminant>0 then go on with calculation, else imaginary number?~Kaeloc
Scratch that...nevermind.~Kaeloc
Ok I've finished it...
Thanks fastmike with the examples...it helped me see exactly what I was trying to do. I didn't use your boolean but seeing how you coded your program helped me simplify my variables. Dukes awarded.
jverd, your math help was invaluable. You really clarified some of my problems. Thanks, Duke to you as well.
~Kaeloc
import cs1.Keyboard;
class QuadraticEquation {
public static void main(String[] args)
throws java.io.IOException
{
double a, b, c;
double x1, x2, discriminant;
System.out.print("Enter a:");
a = cs1.Keyboard.readDouble();
System.out.print("Enter b:");
b = cs1.Keyboard.readDouble();
System.out.print("Enter c:");
c = cs1.Keyboard.readDouble();
discriminant = (b * b - 4 * a * c);
x1 = ( -b + Math.sqrt(discriminant)) / (2 * a);
x2 = ( -b - Math.sqrt(discriminant)) / (2 * a);
if (a==0)
{
x1 = -c/b;
System.out.println("Solution to equation " + a + "x^2 + " + b + "x + " + c + " = 0"+ " is " + x1);
}
else if (discriminant > 0)
{
System.out.println("Solutions to equation " + a + "x^2 + " + b + "x + " + c + " = 0 are " + x1 + " and " + x2);
}
else if (discriminant < 0)
{
System.out.println("Solution to equation " + a + "x^2 + " + b + "x + " + c + " = 0"+ " is not real");
}
else if (x1 == x2)
{
System.out.println("Solution to equation " + a + "x^2 + " + b + "x + " + c + " = 0"+ " is " + x1);
}
}
}
glad i can help. you can divide your program into methods if you want. Thanks for the dukes.
Um, shouldn't you hold off setting the x1 and x2 until after you determine that a is non-zero?
jverda at 2007-7-21 17:49:00 >

Did embla (finally) get the boot? See reply 10, I'm pretty sure that was one of her replies.
> Right so I understand all of that, but if a=0, b=3,
> and c=6 then the answer is undefined or Infinity
> right? You can't divide by zero.
>
> ~Kaeloc
No, it's not undefined, it's just not a quadratic equation so you shouldn't solve the equation with the quadratic formula.
(didn't notice that this thread had 2 pages)
Message was edited by:
Ruly-o_O
Sorry if this is somewhat tangential, but this raises an interesting question:How does Java's Math class deal with imaginary/complex numbers?They're darn useful to have...Joe
> How does Java's Math class deal with> imaginary/complex numbers?It doesn't.
jverda at 2007-7-21 17:49:00 >

> Sorry if this is somewhat tangential, but this raises an interesting question:
>
> How does Java's Math class deal with imaginary/complex numbers?
It can't.
> They're darn useful to have...
Yep, even C has them, C++ has them but if you're using Java you're
stuck with that clumsy "public class Complex { ... }" stuff. And *no*
operator overloading available ;-)
kind regards,
Jos (runs -> that a'way)
JosAHa at 2007-7-21 17:49:04 >

C doesn't even have them that well actually ...
ejpa at 2007-7-21 17:49:04 >

> C doesn't even have them that well actually ...
Starting with C99 it does. You can write for example#include <complex.h>
#include <stdio.h>
int main(void)
{
double complex interesting = cpow(I, 0.5);
printf("%g + %gi\n", creal(interesting), cimag(interesting));
}
Output:> ./a.out
0.707107 + 0.707107i