Home > Archive > Java Help > January 2007 > How to fix possible loss of precision
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to fix possible loss of precision
|
|
| f2prateek 2007-01-17, 8:12 am |
| 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 2007-01-17, 10:07 pm |
| f2prat wrote:
Hi again. Welcome to comp.lang.java.help!
Note that even here, folks are expected to put some
work in, themselves, so I will make one point..
> My program is:
Not indented. When you post code to usenet,
please replace any tabs with 2 or 3 spaces, but
ensure the code is indented.
Something like this..
//program to find the roots of a quadratic eqaution
public class quadratic_roots
{
int 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);
}
}
...indenting code makes it much easier for
someone to read, and you would not want
to make it *hard* for us to read, would you?
Now - I have not addressed your latest problem
yet, because there are a few things I want to
clarify first.
1) Have you found the JavaDocs and Java Tutorial?
(Put the links you found..)
2) Why are you still importing the java.io package
in your code? As others had mentioned on the
thread on comp.lang.java.programmer, it is not
needed for this program.
Andrew T.
| |
| f2prateek 2007-01-17, 10:07 pm |
| You are on the few people I have met who use their brains.I like that.
1)Not any satisfactory results but:-
-http://www.jdocs.com
2)Well the board exams(my tenth grade examinations) insist upon
following the format,and if this program does come in the exam,I have
to follow the method.
| |
| f2prateek 2007-01-17, 10:07 pm |
| Thanks a lot for your help.I figured the right program.All I did was
change r1 and r2 data types to double.
| |
| Andrew Thompson 2007-01-17, 10:07 pm |
| f2prat wrote:
...
> 1)Not any satisfactory results but:-
> -http://www.jdocs.com
OK - since you made an attempt, I'll give you the specific URL's
<http://java.sun.com/j2se/1.5.0/docs...ew-summary.html>
This is the 'Overview Summary' of the on-line version of
J2SE 1.5 JavaDocs. The 1.6 JavaDocs should become
browsable at Sun soon, but I have not located a link
for them yet.
Note that you are better off downloading them,
thay can be found ..here.
<http://java.sun.com/javase/downloads/index_jdk5.jsp>
The Java Tutorial, is also on Sun's site.
<http://java.sun.com/docs/books/tutorial/>
> 2)Well the board exams(my tenth grade examinations) insist upon
> following the format,and if this program does come in the exam,I have
> to follow the method.
umm... OK, ..maybe. But, what you could do is this.
- Get used to writing your code with normal
tabs (or spaces) to indicate indentation.
- Remember to do a 'search/replace' on tabs
for spaces, if posting to usenet, and..
- Remember to do a 'search/replace' on tabs
or groups of spaces, to remove them before
handing in your work.
But I almost bet the tutor's will approve
of indentation, if you ask them.
(If they insist on 'no indentation', they are
making it hard for the students, themselves,
and the learning process!)
BTW - noticed you sorted the 'loss of precision' -
good work.
Andrew T.
| |
| Hendrik Maryns 2007-01-17, 10:07 pm |
| -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
f2prat schreef:
> Thanks a lot for your help.I figured the right program.All I did was
> change r1 and r2 data types to double.
Indeed. You read a tutorial. And if you tutors insist on importing
java.io.*, you can probably make good points by pointing out to them
that is unnecessary. Of course, you’ll have to make a point on *why*
this is unnecessary.
And please quote some context if you reply to a message. Posting
through Google is not an excuse.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFFrlSme+7xMGD3itQRAr10AJ0Thicdpd18
Ygu4KlKG+jqPe1briwCdEba+
pfutFGYV5C+mdRmdZJ307mc=
=eczz
-----END PGP SIGNATURE-----
| |
| Oliver Wong 2007-01-19, 7:08 pm |
| "Hendrik Maryns" <hendrik_maryns@despammed.com> wrote in message
news:eolkb6$7lu$1@newsserv.zdv.uni-tuebingen.de...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> f2prat schreef:
>
> Indeed. You read a tutorial. And if you tutors insist on importing
> java.io.*, you can probably make good points by pointing out to them
> that is unnecessary. Of course, you'll have to make a point on *why*
> this is unnecessary.
Or make an enemy... you know, if they're one of those people who don't
like to get corrected by someone they perceive as socially inferior...
- Oliver
|
|
|
|
|