Hi,
> Sorry i did not post the error my program.The error it is showing is it
> needs a ';' and higlights the line:
>
> r1=-b/2a+(math.sqrt(b*b-4ac))/2a;
Note that (besides the other errors), you must - in contradiction to the
mathematical notation - explicitely write the multiplication *!
Ciao,
Ingo
> Sorry i did not post the error my program.The error it is showing is it
> needs a ';' and higlights the line:
>
> r1=-b/2a+(math.sqrt(b*b-4ac))/2a;
You do not seem to be listenning carefully to what
you are being told. I recall Daniel mentioned that
'math.sqrt' should be 'Math.sqrt'.
Why did you not check that method in the JavaDocs?
(Have you heard of the JavaDocs?)
The line should be more like this, in any case..
r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
Note that '2a' is not understood by Java as
being 2*a the programmer needs to write
the '*' specifically.
It also helps to use more brackets, in order
to ensure that the multiplications and divisions
are done in the order you wish. I may have
got the brackets wrong in the above example.
Then, when those problems are fixed, you
need to fix a 'possible loss of precision'.
It seems you are very new to Java. A good
group for those new to Java is comp.lang.java.help.
Also, if you haven't discovered them yet,
look around for the JavaDocs and the Java
tutorial. I think both would be a great help
to you.
You were given a number of good tips by
the other replies on this thread - please
review them all carefully.
HTH
Andrew T.
f2prateek - 17 Jan 2007 13:41 GMT
I have tried all the variations you have told me,including the 'M'
capital in Math.sqrt .I accidentally omitted the * in the
multiplication calculation in the program i submitted to you.
f2prateek - 17 Jan 2007 13:42 GMT
//program to find the roots of a quadratic eqaution
import java.io.*;
public class quadratic_roots
{
float r1,r2;
public void accept(int a,int b,int c)
{
r1=-b/2*a+(Math.sqrt(b*b-4*a*c))/2*a;
r2=-b/2*a-(Math.sqrt(b*b-4*a*c))/2*a;
System.out.println(r1+" , "+r2);
}
}
now it's showing me possible loss of precision in the line:
r1=-b/2*a+(Math.sqrt(b*b-4*a*c))/2*a;
f2prateek - 17 Jan 2007 13:45 GMT
Sorry.Some typing mistake again.
My program is:
//program to find the roots of a quadratic eqaution
import java.io.*;
public class quadratic_roots
{
float r1,r2;
public void accept(int a,int b,int c)
{
r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
r2=-b/(2*a)-(Math.sqrt(b*b-4*(a*c)))/(2*a);
System.out.println(r1+" , "+r2);
}
}
It is showing me 'possible loss of precision' in the line:
r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
Andrew Thompson - 17 Jan 2007 13:55 GMT
> Sorry.Some typing mistake again.
You are doing a number of mistakes at the
moment in terms of getting people to *want*
to help you.
1st of all - do not waste our time with typing
mistakes.
Always copy/paste both code and compiler
or runtime errors. *Always*.
Next - you seem to have ignored me when I
asked you to review this thread carefully.
You are still making mistakes that others
have already mentioned, and it is obvious
from your flurry of replies that you did not
even read my post carefully.
As such - I will make no further posts on
this thread.
See you over on c.l.j.help, if you should
choose to post there.
Andrew T.
tam@lheapop.gsfc.nasa.gov - 17 Jan 2007 14:34 GMT
> Sorry.Some typing mistake again.
> My program is:
[quoted text clipped - 14 lines]
> It is showing me 'possible loss of precision' in the line:
> r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
You've gotten far enough along here that you're starting to deal with
errors that are a bit more specific to Java that to programming in
general,
but you really need to get some background in basic elements of the
language.
You've got two immediate things to worry about. The first term on the
right of the
equality involves integer division. This likely doesn't do what you
want in Java.
E.g., what do you think
int x = 3
double y = x/2;
System.out.println(y)
prints out? It's not 1.5.
The second issue-- which is causing your current error message --
involves
what happens in a computer program when the arguments to an operator
involves different data types. In Java Math.sqrt always returns a
'double'.
You are adding this to an 'int' value. Java's rule is that the integer
argument
is converted to a double and you get a double result. In the end you
try
to save this as a float (in r1 or r2). Since floats have less
precision than
doubles, Java does not allow you to do this without an explicit cast.
There are some other issues you may want to think about -- what happens
when the discriminant is negative? But before getting into such
subtleties
you need to spend some time learning basic Java to understand how
it does arithmetic and coversions amongst the arithmetic types.
Regards,
Tom McGlynn
Alex Hunsley - 18 Jan 2007 16:32 GMT
>> Sorry i did not post the error my program.The error it is showing is it
>> needs a ';' and higlights the line:
[quoted text clipped - 11 lines]
>
> r1=-b/(2*a)+(Math.sqrt(b*b-4*(a*c)))/(2*a);
Yup, it's correct, but one inefficiency that can be taken out is the
dividing by 2a twice, since:
-b sqrt(b^2 - 4ac)
-- +/- ----------------
2a 2a
is equal to:
-b +/- sqrt(b^2 - 4ac)
---------------------
2a
lex