Results 1 to 20 of 21
Thread: Kwadratic formula
 03262012, 11:19 AM #1Member
 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*38*xxx+4;
}
if (equation ==0){
System.out.println(xxx);
}
}
}
Im using Netbeans ...if it matters.
 03262012, 11:24 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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*b4*a*c))/(2*a).
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03262012, 11:38 AM #3Member
 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.
 03262012, 11:41 AM #4Member
 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 ?
 03262012, 11:55 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Re: Kwadratic formula
Sure, add 0.01 to variable xxx each time through the loop:
Java Code:for (double xxx= 1000; x < 1000; x+= 0.01) // ...
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03262012, 11:59 AM #6Member
 Join Date
 Mar 2012
 Posts
 6
 Rep Power
 0
 03262012, 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
 03262012, 12:32 PM #8Member
 Join Date
 Mar 2012
 Posts
 6
 Rep Power
 0
Re: Kwadratic formula
Uhm , in nooblanguage please. Or most preferably in code form ???
 03262012, 12:34 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 03262012, 12:39 PM #10Member
 Join Date
 Mar 2012
 Posts
 6
 Rep Power
 0
 03262012, 12:51 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
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 JenkinsTraub method that works on arbitrary polynomials ...
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03262012, 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,
Java Code:for(int xxx=0;xxx<1;xxx+=0.01) { System.out.println(xxx); }
(first 15 output values..)
Java Code:0.0 0.01 0.02 0.03 0.04 0.05 0.060000000000000005 //this is nonexact 0.07 0.08 0.09 0.09999999999999999//nonexact 0.10999999999999999//nonexact 0.11999999999999998//nonexact 0.12999999999999998//nonexact 0.13999999999999999//nonexact
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; 03262012 at 05:13 PM.
 03262012, 01:32 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
Re: Kwadratic formula
Of course you haven't read my original reply:
Originally Posted by JosAH
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03262012, 01:37 PM #14
Re: Kwadratic formula
 03262012, 02:32 PM #15
Re: Kwadratic formula
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; } } } } } } }
Java Code:0.6666666666666666 2.0
regards
dhilipLast edited by noobplus; 03262012 at 02:40 PM.
 03262012, 02:33 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 03262012, 02:36 PM #17
Re: Kwadratic formula
 03262012, 03:11 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,299
 Blog Entries
 7
 Rep Power
 24
 03262012, 03:17 PM #19
Re: Kwadratic formula
"Kwadratic" is my new favorite word.
How to Ask Questions the Smart Way
Static Void Games  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 03262012, 03:20 PM #20
Re: Kwadratic formula
Last edited by noobplus; 03262012 at 03:26 PM.
Similar Threads

Formula Logic Help
By sehudson in forum New To JavaReplies: 4Last Post: 03112011, 05:17 AM 
Formula class
By imorio in forum New To JavaReplies: 3Last Post: 02232011, 10:38 PM 
changing TF formula
By o.imen in forum LuceneReplies: 0Last Post: 09162010, 01:20 PM 
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07032009, 06:57 PM 
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04302007, 10:00 PM
Bookmarks