FAQ  Donate 
Members List  
Calendar  
Search  
Today's Posts 
LPF Laser Pointer Company Database 

LPF Database of Laser Pointer Companies 
 LinkBack  Thread Tools  Display Modes 
10232009, 08:45 PM  #17  
 
Junior Member Join Date: Oct 2009 Location: USAIN
Posts: 6
Rep Power: 0 
Personally I don't feel that java would be a good choice for this type of application, unless you're doing a web page. Pascal is ideal for this type of coding, as you can code and test as you go. Instant feedback. Whenever I need a small program to do something, I write it in pascal, makes very fast work of small problems. You should be aware of several things: get a copy of the swag code: Main SWAG (Sourceware Archive Group) HTML Index contains lots of useful pascal code snippets. You also should consider having a copy of the Free Pascal compiler, its free and available for most CPUs. Now, back to the problem. It's interesting, I haven't done the math for years so I checked wikipedia (don't right now, its wrong!) and wrote this: (let me know what you think, I working on a version that will allow you to change the amount of decimals in the answer on the fly!) program Quadratics; uses crt; const reada = 'Please enter the value of a '; readb = 'Please enter the value of b '; readc = 'Please enter the value of c '; var e: integer; a2, x, x1, x2, ix, a, b, c, d, z: real; ch: char; done: boolean; function getReal (s: string): real; var r: real; rs: string; begin repeat write (s); readln (rs); val (rs, r, e); if (e <> 0) then writeln ('Input must be a real number!'); until (e = 0); getReal := r; end; function noZero (r: real; n: integer): string; var s: string; begin str (r:0:n, s); while (s[length (s)] = '0') do delete (s, length (s), 1); if (s[length (s)] = '.') then delete (s, length (s), 1); noZero := s; end; begin textColor (yellow); done := false; clrscr; Writeln ('Hi, I am the Quadratic Calculator!'); writeln ('Press enter to get started'); ch := readKey; clrscr; while not done do begin writeln; textColor (lightGreen); writeln ('Input the values for the Quadratic equation!'); textColor (yellow); writeln; Writeln ('Please use this as a guide! ax^2 + bx + c'); writeln; a := getReal (reada); b := getReal (readb); c := getReal (readc); d := (b * b)  (4 * a * c); { find the discriminant } a2 := 2 * a; writeln; if (d = 0) then begin { discriminant is zero, one real root } x1 := b / a2; writeln ('x = ', noZero (x1, 3)); end else if (d > 0) then begin { discriminant is positive, there are 2 roots } x1 := (b + sqrt (d)) / a2; x2 := (b  sqrt (d)) / a2; writeln ('x = ', noZero (x1, 3), ', ', noZero (x2, 3)); end else begin { discriminant is negative, roots are complex } x1 := b / a2; ix := sqrt (abs ((b * b)  4 * a * c)) / a2; writeln ('x = ', noZero (x1, 3), ' + i', noZero (ix, 3), ', ', noZero (x1, 3), '  i', noZero (ix, 3)); end; writeln; { x := (b) + Sqrt ((b * b)  (4 * a * c)); x := x / 2 * a; x2 := (b)  Sqrt (((b * b)  (4 * a * c))); x2 := x2 / 2 * a; clrscr; writeln ('WOW THAT WAS EASY!'); writeln; write ('x= '); write (x:2:2); write (' or '); writeln (x2:2:2); writeln; writeln ('Type any number and press enter to exit!'); readln (z); } writeln ('Esc to quit, any other key to go again'); ch := readkey; if (ch = #27) then done := true; end; end.  
10232009, 10:42 PM  #18  
 
Class 3B Laser Join Date: May 2009 Location: Sydney, Australia
Posts: 3,256
Rep Power: 272 
Hey thats great... one problem tho.. out put looks like this (a=3 b=7 c=9) Code: x= 1.167 + i1.28, 1.167  i1.28
__________________ Lasers! 10.6um  >40W CO2 Laser  25W RF excited CO2 Laser* 685nm  Being built! 660nm  250mW LOC PowerLight B50 Host  100mW WL pulsar 635nm  430mW C6 Host* 632.8nm  Red HeNe laser ~1mW 593.5nm  Regal 5 542.8nm  Green HeNe laser ~2.5mW 532nm  620mW+ lasersman Firedragon III*  100mW PGLIIIM from JETLASERS*  70mW Rayfoss Host* ~~~  175mW Rayfoss Host  <5mW WL Core  55mW DinoDirect review laser* 488nm  On the hoist 473nm  2x CNI lab lasers ~40mW 445nm  3W Big Boyz Laser  >1.1W Cree C6 Host  650mW WL Arctic 405nm  65mW GLP405 Aquarius host  160mW 6x Bluray MXDL Host Total: 71,868.5 mW Goggles! Laser glow LSG532Sport protective eye goggles OD 7+ 190532nm HIGHLY RECOMMENDED!! Jetlasers wrap around 190540nm OD4+ goggles* Eagle Pair Red OD4+ goggles Scopeguy20 Blublockers for 405nm lasers! * indicates a review has been done, on this item. Check out my How to Build a Selectable Dummy Load Thread!  
10252009, 03:21 AM  #19  
 
Junior Member Join Date: Oct 2009 Location: USAIN
Posts: 6
Rep Power: 0 
But that's not a problem, that's the answer! The "i" represents the square root of 1. Whenever the determinant (b*b4ac) is negative, the answer will be complex. A complex number has two parts, a part called the real part and a part called the complex part. Your program was stopping because you were asking it to compute the square root of a minus number, turbo pascal sqrt function cannot do this. If you were to do it with a scientific calculator (like the TI89) in complex mode it would also return the x+iy type of answer. By moving the 1 out of the square root radical (the "i") the determinant is now positive and the result can be calculated. When the determinant is negative, there are always two answers. One with the form x+iy and one with the form xiy. Check out the wikipedia site on quadratic equation, there is much information there.  
10252009, 03:23 AM  #20  
 
Junior Member Join Date: Oct 2009 Location: USAIN
Posts: 6
Rep Power: 0 
I wrote that program in about an hour before I sent it. That's what I love about pascal, and you end up with an executable.
 
10252009, 03:25 AM  #21  
 
Junior Member Join Date: Oct 2009 Location: USAIN
Posts: 6
Rep Power: 0 
Complex numbers were invented many years before there was a physical use for them. AC voltage/current calculations use them, amoung many others.
 
10252009, 04:04 AM  #22  
 
Class 3B Laser Join Date: May 2009 Location: Sydney, Australia
Posts: 3,256
Rep Power: 272 
OHHH I see.. yes I understand now! I just thought it was a bug! Kool! thanks! +rep!
__________________ Lasers! 10.6um  >40W CO2 Laser  25W RF excited CO2 Laser* 685nm  Being built! 660nm  250mW LOC PowerLight B50 Host  100mW WL pulsar 635nm  430mW C6 Host* 632.8nm  Red HeNe laser ~1mW 593.5nm  Regal 5 542.8nm  Green HeNe laser ~2.5mW 532nm  620mW+ lasersman Firedragon III*  100mW PGLIIIM from JETLASERS*  70mW Rayfoss Host* ~~~  175mW Rayfoss Host  <5mW WL Core  55mW DinoDirect review laser* 488nm  On the hoist 473nm  2x CNI lab lasers ~40mW 445nm  3W Big Boyz Laser  >1.1W Cree C6 Host  650mW WL Arctic 405nm  65mW GLP405 Aquarius host  160mW 6x Bluray MXDL Host Total: 71,868.5 mW Goggles! Laser glow LSG532Sport protective eye goggles OD 7+ 190532nm HIGHLY RECOMMENDED!! Jetlasers wrap around 190540nm OD4+ goggles* Eagle Pair Red OD4+ goggles Scopeguy20 Blublockers for 405nm lasers! * indicates a review has been done, on this item. Check out my How to Build a Selectable Dummy Load Thread!  
10262009, 10:44 PM  #23  
 
Junior Member Join Date: Oct 2009 Location: USAIN
Posts: 6
Rep Power: 0 
Here's the code for the improved program, let's you change the decimals on the fly. program Quadratics; uses crt; const reada = 'Please enter the value of a: '; readb = 'Please enter the value of b: '; readc = 'Please enter the value of c: '; var e: integer; a2, x, x1, x2, ix, a, b, c, d, z: real; ch: char; done: boolean; function getReal (s: string): real; var r: real; rs: string; begin repeat write (s); readln (rs); val (rs, r, e); if (e <> 0) then writeln ('Input must be a real number!'); until (e = 0); getReal := r; end; function noZero (r: real; n: integer): string; var s: string; begin str (r:0:n, s); while (s[length (s)] = '0') do delete (s, length (s), 1); if (s[length (s)] = '.') then delete (s, length (s), 1); noZero := s; end; var t, y: integer; s: string; next: boolean; begin textColor (yellow); done := false; clrscr; Writeln ('Quadratic Calculator!'); writeln; writeln (' If there are decimals, you may press the "+" and "" keys to change'); writeln (' the amount of decimals shown or any other keys to continue'); writeln; writeln ('Press enter to get started'); writeln; ch := readKey; clrscr; while not done do begin writeln; textColor (lightGreen); writeln ('Input the values for the Quadratic equation!'); textColor (yellow); writeln; Writeln ('Please use this as a guide! ax^2 + bx + c'); writeln; a := getReal (reada); b := getReal (readb); c := getReal (readc); d := (b * b)  (4 * a * c); { find the discriminant } a2 := 2 * a; writeln; if (d = 0) then begin { discriminant is zero, one real root } x1 := b / a2; writeln ('x = ', noZero (x1, 3)); end else if (d > 0) then begin { discriminant is positive, there are 2 roots } x1 := (b + sqrt (d)) / a2; x2 := (b  sqrt (d)) / a2; writeln ('x = ', noZero (x1, 3), ', ', noZero (x2, 3)); end else begin { discriminant is negative, roots are complex } x1 := b / a2; ix := sqrt (abs ((b * b)  4 * a * c)) / a2; next := false; t := 3; repeat s := 'x = ' + noZero (x1, t) + ' + i' + noZero (ix, t) + ', ' + noZero (x1, t) + '  i' + noZero (ix, t); y := length (s); s := s + copy (' ', 1, 70y) + #13; write (s); ch := readKey; case ch of '+': begin inc (t); if (t > 9) then t := 9; end; '': begin dec (t); if (t < 0) then t := 0; end else next := true; end; until (next); writeln; end; writeln; writeln ('Esc to quit, any other key to go again'); ch := readkey; if (ch = #27) then done := true; end; end. Have fun. Paul  
10262009, 11:02 PM  #24  
 
Junior Member Join Date: Oct 2009 Location: USAIN
Posts: 6
Rep Power: 0 
Oops, one small problem, try this instead: program Quadratics; uses crt; const reada = 'Please enter the value of a: '; readb = 'Please enter the value of b: '; readc = 'Please enter the value of c: '; var e: integer; a2, x, x1, x2, ix, a, b, c, d, z: real; ch: char; done: boolean; function getReal (s: string): real; var r: real; rs: string; begin repeat write (s); readln (rs); val (rs, r, e); if (e <> 0) then writeln ('Input must be a real number!'); until (e = 0); getReal := r; end; function noZero (r: real; n: integer): string; var s: string; begin str (r:0:n, s); while (s[length (s)] = '0') do delete (s, length (s), 1); if (s[length (s)] = '.') then delete (s, length (s), 1); noZero := s; end; var t, y: integer; s: string; next: boolean; begin textColor (yellow); done := false; clrscr; Writeln ('Quadratic Calculator!'); writeln; writeln (' If there are decimals, you may press the "+" and "" keys to change'); writeln (' the amount of decimals shown or any other keys to continue'); writeln; writeln ('Press enter to get started'); writeln; ch := readKey; clrscr; while not done do begin writeln; textColor (lightGreen); writeln ('Input the values for the Quadratic equation!'); textColor (yellow); writeln; Writeln ('Please use this as a guide! ax^2 + bx + c'); writeln; a := getReal (reada); b := getReal (readb); c := getReal (readc); d := (b * b)  (4 * a * c); { find the discriminant } a2 := 2 * a; writeln; next := false; t := 3; if (d = 0) then begin { discriminant is zero, one real root } x1 := b / a2; repeat s := 'x = ' + noZero (x1, t); y := length (s); s := s + copy (' ', 1, 30y) + #13; write (s); ch := readKey; case ch of '+': begin inc (t); if (t > 9) then t := 9; end; '': begin dec (t); if (t < 0) then t := 0; end else next := true; end; until (next); writeln; end else if (d > 0) then begin { discriminant is positive, there are 2 roots } x1 := (b + sqrt (d)) / a2; x2 := (b  sqrt (d)) / a2; repeat s := 'x = ' + noZero (x1, t) + ', ' + noZero (x2, t); y := length (s); s := s + copy (' ', 1, 40y) + #13; write (s); ch := readKey; case ch of '+': begin inc (t); if (t > 9) then t := 9; end; '': begin dec (t); if (t < 0) then t := 0; end else next := true; end; until (next); writeln; end else begin { discriminant is negative, roots are complex } x1 := b / a2; ix := sqrt (abs ((b * b)  4 * a * c)) / a2; repeat s := 'x = ' + noZero (x1, t) + ' + i' + noZero (ix, t) + ', ' + noZero (x1, t) + '  i' + noZero (ix, t); y := length (s); s := s + copy (' ', 1, 70y) + #13; write (s); ch := readKey; case ch of '+': begin inc (t); if (t > 9) then t := 9; end; '': begin dec (t); if (t < 0) then t := 0; end else next := true; end; until (next); writeln; end; writeln; writeln ('Esc to quit, any other key to go again'); ch := readkey; if (ch = #27) then done := true; end; end. Paul  
10282009, 03:14 AM  #25  
 
Class 3B Laser Join Date: May 2009 Location: Sydney, Australia
Posts: 3,256
Rep Power: 272 
Got it!! NICE JOB!! Thanks heepz! Adrian
__________________ Lasers! 10.6um  >40W CO2 Laser  25W RF excited CO2 Laser* 685nm  Being built! 660nm  250mW LOC PowerLight B50 Host  100mW WL pulsar 635nm  430mW C6 Host* 632.8nm  Red HeNe laser ~1mW 593.5nm  Regal 5 542.8nm  Green HeNe laser ~2.5mW 532nm  620mW+ lasersman Firedragon III*  100mW PGLIIIM from JETLASERS*  70mW Rayfoss Host* ~~~  175mW Rayfoss Host  <5mW WL Core  55mW DinoDirect review laser* 488nm  On the hoist 473nm  2x CNI lab lasers ~40mW 445nm  3W Big Boyz Laser  >1.1W Cree C6 Host  650mW WL Arctic 405nm  65mW GLP405 Aquarius host  160mW 6x Bluray MXDL Host Total: 71,868.5 mW Goggles! Laser glow LSG532Sport protective eye goggles OD 7+ 190532nm HIGHLY RECOMMENDED!! Jetlasers wrap around 190540nm OD4+ goggles* Eagle Pair Red OD4+ goggles Scopeguy20 Blublockers for 405nm lasers! * indicates a review has been done, on this item. Check out my How to Build a Selectable Dummy Load Thread!  
Thread Tools  
Display Modes  
 