Results 1 to 20 of 21
Thread: Kwadratic formula
- 03-26-2012, 11:19 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Kwadratic formula
Hi Im quite new to java and programming in general and I was hoping on getting some help with this piece of code.
class kwadratic {
public static void main(String []args){
for (double xxx=-1000; xxx<1000;xxx++){
double equation = xxx*xxx*3-8*xxx+4;
}
if (equation ==0){
System.out.println(xxx);
}
}
}
Im using Netbeans ...if it matters.
- 03-26-2012, 11:24 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Kwadratic formula
That way you'll only find one root (2.0) if you're lucky; the other root (2/3) can not be found that way. Why don't you apply the close form formula (-b+-sqrt(b*b-4*a*c))/(2*a).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-26-2012, 11:38 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Kwadratic formula
Well I wanted to find the solution without the formula to make it as basic as possible.
- 03-26-2012, 11:41 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Kwadratic formula
Is there any way of letting the xxx increment by ..lets say 0.01 instead of 1 ?
- 03-26-2012, 11:55 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Kwadratic formula
Sure, add 0.01 to variable xxx each time through the loop:
... but you still won't find the other root (2/3) that way.Java Code:for (double xxx= -1000; x < 1000; x+= 0.01) // ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-26-2012, 11:59 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
- 03-26-2012, 12:25 PM #7
Re: Kwadratic formula
hi,
a variable cannot be accessed outside a block
also, every instance of the variable "equation" should be checked, but u're checking it only once after the end of the loop
regards
dhilip
- 03-26-2012, 12:32 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
Re: Kwadratic formula
Uhm , in nooblanguage please. Or most preferably in code form ???
- 03-26-2012, 12:34 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 03-26-2012, 12:39 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 6
- Rep Power
- 0
- 03-26-2012, 12:51 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Kwadratic formula
So you're happy with this? Your program only finds solutions to your quadratic formula if the root is -1000+i*0.01. An ordinary quadratic formula isn't too difficult to implement ... it's not like a Jenkins-Traub method that works on arbitrary polynomials ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-26-2012, 01:27 PM #12
Re: Kwadratic formula
Hi,
adding "0.01" to "double xxx=0" gives us approximate double values, not exact increment
for example,
gives outputJava Code:for(int xxx=0;xxx<1;xxx+=0.01) { System.out.println(xxx); }
(first 15 output values..)
this is because of rounding errorsJava Code:0.0 0.01 0.02 0.03 0.04 0.05 0.060000000000000005 //this is non-exact 0.07 0.08 0.09 0.09999999999999999//non-exact 0.10999999999999999//non-exact 0.11999999999999998//non-exact 0.12999999999999998//non-exact 0.13999999999999999//non-exact
hence even "2.0" may not be displayed
I learnt it at Java theory and practice: Where's your point?
regards
dhilipLast edited by noobplus; 03-26-2012 at 05:13 PM.
- 03-26-2012, 01:32 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Kwadratic formula
Of course you haven't read my original reply:
Bold faced font added for clarity.
Originally Posted by JosAH
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-26-2012, 01:37 PM #14
Re: Kwadratic formula
- 03-26-2012, 02:32 PM #15
Re: Kwadratic formula
output:Java Code:/** *@author:dhilipkumar(noobplus) */ public class kwadratic { static double equation; static double incrementedvalue; static double repeatchecker=-1; public static void main(String []args){ for(int x=0;x<11;x++) { for(int i=1;i<11;i++) { for(int j=0;j<10;j++) { if(j<i) { incrementedvalue=(x+(double)j/(double)i); equation = (incrementedvalue*incrementedvalue*3)-(8*incrementedvalue)+4; if ((equation==0) && (incrementedvalue!=repeatchecker)) { System.out.println(incrementedvalue); repeatchecker=incrementedvalue; } } } } } } }
we don't need to use "xxx+=0.01"Java Code:0.6666666666666666 2.0
-.gif)
regards
dhilipLast edited by noobplus; 03-26-2012 at 02:40 PM.
- 03-26-2012, 02:33 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 03-26-2012, 02:36 PM #17
Re: Kwadratic formula
- 03-26-2012, 03:11 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 03-26-2012, 03:17 PM #19
Re: Kwadratic formula
"Kwadratic" is my new favorite word.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-26-2012, 03:20 PM #20
Re: Kwadratic formula
Last edited by noobplus; 03-26-2012 at 03:26 PM.
Similar Threads
-
Formula Logic Help
By sehudson in forum New To JavaReplies: 4Last Post: 03-11-2011, 04:17 AM -
Formula class
By imorio in forum New To JavaReplies: 3Last Post: 02-23-2011, 09:38 PM -
changing TF formula
By o.imen in forum LuceneReplies: 0Last Post: 09-16-2010, 01:20 PM -
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07-03-2009, 06:57 PM -
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04-30-2007, 10:00 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote



Bookmarks